Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Aman Kumar
validation
2 Answer(s)      5 years and 8 months ago
Posted in : Swing AWT



Advertisement
View Answers

May 1, 2009 at 4:43 PM


Hi Friend,

We are providing you a code that validates the fields of registration form:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.regex.*;
class Form extends JFrame
{
JButton ADD;
JPanel panel;
JLabel label1,label2,label3,label4,label5,label6;
final JTextField text1,text2,text3,text4,text5,text6;
Form() {
label1 = new JLabel();
label1.setText("User Name:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("First Name:");
text2 = new JTextField(20);
label3 = new JLabel();
label3.setText("Last Name:");
text3 = new JTextField(20);
label4 = new JLabel();
label4.setText("Password:");
text4 = new JPasswordField(20);
label5 = new JLabel();
label5.setText("Email:");
text5 = new JTextField(20);
label6 = new JLabel();
label6.setText("Address:");
text6 = new JTextField(20);
ADD=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(7,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(label6);
panel.add(text6);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
String value5=text5.getText();
String value6=text6.getText();
Pattern p = Pattern.compile("^[a-zA-Z0-9]+$");
Matcher m1 = p.matcher(value1);
boolean f1 = m1.matches();
Matcher m2 = p.matcher(value2);
boolean f2 = m2.matches();
Matcher m3 = p.matcher(value3);
boolean f3 = m3.matches();
Matcher m4 = p.matcher(value4);
boolean f4 = m1.matches();

Pattern p5=Pattern.compile("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$");
Matcher m5=p5.matcher(value5);
boolean f5=m5.matches();
if((f1==false)||(value1.length()<6)|| (value1.length()>15)){
JOptionPane.showMessageDialog(null,"Invalid User Name");
}
else if((f2==false)||(value2.length()<6)||( value2.length()>15)){
JOptionPane.showMessageDialog(null,"Invalid First Name");
}
else if((f3==false)||(value3.length()<6)||( value3.length()>15)){
JOptionPane.showMessageDialog(null,"Invalid Last Name");
}
else if((f4==false)||(value4.length()<6)|| (value4.length()>15)){
JOptionPane.showMessageDialog(null,"Invalid Password");
}
else if(f5==false) {
JOptionPane.showMessageDialog(null,"Invalid Email Id");
}
else if(value6.length()==0) {
JOptionPane.showMessageDialog(null,"Invalid Address");
}
else{
CreateForm tab=new CreateForm();
tab.setVisible(true);
JLabel label = new JLabel("You are successfully registered");
tab.getContentPane().add(label);
}
}
});
}
}
class FormDemo{
public static void main(String arg[]) {
try {
Form frame=new Form();
frame.setSize(300,300);
frame.setVisible(true);
}
catch(Exception e) {}
}
}


May 1, 2009 at 4:45 PM


continue...............

In Java Swings,you can give link from one page to another by creating an object of another existed java swing class.As we have used the class 'CreateForm.java' to show the success message.

Here is the code of 'CreateForm.java:

import javax.swing.*;
import java.awt.*;

class CreateForm extends JFrame{
CreateForm()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setSize(400, 150);
}
}
Thanks



Related Tutorials/Questions & Answers:
validation
validation  validation
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
NSURL Validation
NSURL Validation  How to validate a NSURL string using special character validation
form validation
form validation  how the form validation is done in jsf form using javaScript
validation query
validation query  where I should kept properties file inside struts Web application
Validation - Struts
Validation  what is the best way to use validation in Struts?either "validation.xml" or JavaScript
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
email validation
email validation  during email validation.... after domain name which it means if example example@gmail.co .... 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 example@gmail.co .... this address allowed by any validation program .... but i want to show invalid email address because domain name com
Date Validation
Date Validation  Hi, I need Date Validation using java in spring framework. Please Anyone help me... Thanks in advance
comboBox validation
comboBox validation  dear sir. i want to know . how to validate radio button in struts using xml validation. thanks
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
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
validation - Framework
validation  how to validate the action forms in struts? could you please explain how cross validation is done for date?  You go the following url: http://www.roseindia.net/struts/struts-login-form.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 - Struts
Validation  How can i use validation framework i don't understand am...; Hi friend, Phone validation using javaScript function... return true; } Phone Validation
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
 

 

 

DMCA.com