validation
we are doing payroll system project in java.So pls provide the complete validation code for employee master form.
View Answers
December 2, 2011 at 4:04 PM
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
class EmployeeForm {
public static boolean validateName(String name) {
return name.matches("[A-Z][a-z]+( [A-Z][a-z]+)?");
}
public static boolean validatePhone(String phone) {
return phone.matches("^[0-9]{10}");
}
public static boolean isEmailValid(String email) {
return email.matches("^[\\w\\-]([\\.\\w])+[\\w][email protected]([\\w\\-]+\\.)+[a-z]{2,4}$");
}
public static void main(String[] args) {
JFrame f=new JFrame();
f.setLayout(null);
JLabel l1=new JLabel("Name: ");
final JTextField text1=new JTextField(20);
JLabel l2=new JLabel("Contact No: ");
final JTextField text2=new JTextField(20);
JLabel l3=new JLabel("Email: ");
final JTextField text3=new JTextField(20);
JButton b=new JButton("Submit");
l1.setBounds(10,10,120,20);
text1.setBounds(150,10,100,20);
l2.setBounds(10,40,120,20);
text2.setBounds(150,40,100,20);
l3.setBounds(10,70,120,20);
text3.setBounds(150,70,100,20);
b.setBounds(150,100,100,20);
f.add(l1);
f.add(text1);
f.add(l2);
f.add(text2);
f.add(l3);
f.add(text3);
f.add(b);
f.setVisible(true);
f.setSize(400,200);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name=text1.getText();
String phone=text2.getText();
String email=text3.getText();
if(validateName(name) != true) {
JOptionPane.showMessageDialog(null,"Invalid Name! Both first and last name is required with first letter in capital.");
text1.setText(" ");
}
else if(validatePhone(phone) != true) {
JOptionPane.showMessageDialog(null,"Invalid PhoneNumber!");
text2.setText(" ");
}
else if(isEmailValid(email) != true) {
JOptionPane.showMessageDialog(null,"Invalid Email!");
text3.setText(" ");
}
else{
JOptionPane.showMessageDialog(null,"All fields have been filled properly.");
}
}
});
}
}
December 2, 2011 at 4:04 PM
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
class Validations extends JFrame{
JLabel l1,l2,l3,l;
JLabel lab1,lab2,lab3;
JTextField t1,t2,t3;
JPanel p;
Validations(){
l1=new JLabel("Enter Date: (MM/dd/yyyy)");
t1=new JTextField(20);
l2=new JLabel("Enter Phone Number: ");
t2=new JTextField(20);
l3=new JLabel("Enter Email: ");
t3=new JTextField(20);
lab1=new JLabel();
lab2=new JLabel();
lab3=new JLabel();
p=new JPanel(new GridLayout(3,3));
p.add(l1);
p.add(t1);
p.add(lab1);
p.add(l2);
p.add(t2);
p.add(lab2);
p.add(l3);
p.add(t3);
p.add(lab3);
add(p);
setVisible(true);
pack();
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String date=t1.getText();
try{
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
df.setLenient(false);
Date dt2 = df.parse(date);
lab1.setVisible(false);
}
catch(Exception ex){
lab1.setText("* Invalid Date");
lab1.setVisible(true);
lab1.setForeground(Color.red);
}
}
});
t2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input = t2.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%[email protected]!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
lab2.setText("* Enter only Numeric Value");
lab2.setVisible(true);
lab2.setForeground(Color.red);
}
else{
lab2.setVisible(false);
}
}
});
t3.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String email=t3.getText();
String exp="^[\\w\\-]([\\.\\w])+[\\w][email protected]([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence seq = email;
Pattern pattern = Pattern.compile(exp,Pattern.CASE_INSENSITIVE);
Matcher m = pattern.matcher(seq);
if (m.matches()) {
lab3.setVisible(false);
}
else {
lab3.setText("* Invalid Email ID");
lab3.setVisible(true);
lab3.setForeground(Color.red);
}
}
});
}
public static void main(String[]args){
Validations v=new Validations();
}
}
December 3, 2011 at 12:54 PM
Thank you so much for ur reply...and also pls provide jsp code for date validation
Ads
Related Tutorials/Questions & Answers:
validation
validation we are doing payroll system project in java.So pls provide the complete
validation code for employee master form
Advertisements
validation.....
validation..... hi..........
thanks for ur reply for
validation code.
but i want a very simple code in java swings where user is allowed to enter only numerical values in textbox , if he enters string values then it should
validation
Validation method valid the in put. otherwise call the javascript onsubmit to check... code.Ok if you use struts then you can use
Validation method valid the in put
Validation
);
JLabel label=new JLabel("JTable
validation Example",JLabel.CENTER);
JPanel panel=new JPanel();
panel.add(scroll);
JFrame frame=new JFrame("JTable
validation
validation
validation please help me to check
validation for
<form>
<table class="form">
<tr>
<td class="col1">
<label>Sno:</label>
</td>
<td
validation files
validation files If I place
validation files in the folder where the action classes are present,will there any error arise
javascript validation
javascript validation
validation of comparing dropdownlist and textbox in javascript
validation query
validation query where I should kept properties file inside struts Web application
form validation
form validation how the form
validation is done in jsf form using javaScript
NSURL Validation
NSURL Validation How to validate a NSURL string using special character
validation
email validation
email validation during email
validation.... after domain name which it means if example
[email protected] .... this address allowed by any
validation program .... but i want to show invalid email address because domain name com
email validation
email validation during email
validation.... after domain name which it means if example
[email protected] .... this address allowed by any
validation program .... but i want to show invalid email address because domain name com
Validation of datepicker
Validation of datepicker I have a datepicker in my JSp...the seleted dates are put in a text box. I want the alert the user from selecting future... a future date is entered..How to do this
validation in javascript or jsp
Form Validation
Form Validation Java script
validation for checking special characters and white spaces and give an alert.Please help me urgent
Thanks in advance
comboBox validation
comboBox validation dear sir.
i want to know . how to validate radio button in struts using xml
validation.
thanks
Date Validation
Date Validation Hi,
I need Date
Validation using java in spring framework. Please Anyone help me...
Thanks in advance
date validation
date validation sir,
pls provide date
validation code in javascript..we want to include it into our master form..
Please visit the following link:
http://www.roseindia.net/mysql/datevalidation.shtml
Struts validation
Struts validation I want to put
validation rules on my project.But after following all the rules I can't find the result.
I have extended...
validation rules,put the plugins inside strutsconfig.xml, put the html:errors tag
Validation doubt
Validation doubt hi.....
thanks for the other
validation code. I have got that and implemented in my code but i have a doubt in that.
As we try to put string values its not allowing to do tht it gives us message its right
Server side validation vs client side validation
Server side
validation vs client side validation Can any one tell me the difference between these two different ways of
Validation? Also features of Server side
validation vs client side
validation.
The client side
Struts2 validation Procedure
Struts2
validation Procedure In struts2 Which one is best XML
Validation or Programatic
validation? and which one is used in real world application
Struts2 validation Procedure
Struts2
validation Procedure In struts2 Which one is best XML
Validation or Programatic
validation? and which one is used in real world application
Struts2 validation Procedure
Struts2
validation Procedure In struts2 Which one is best XML
Validation or Programatic
validation? and which one is used in real world application