Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  




Share on Google+Share on Google+

Aman Kumar
2 Answer(s)      8 years ago
Posted in : Swing AWT
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();
text4 = new JPasswordField(20);
label5 = new JLabel();
text5 = new JTextField(20);
label6 = new JLabel();
text6 = new JTextField(20);
ADD=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(7,2));

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");
CreateForm tab=new CreateForm();
JLabel label = new JLabel("You are successfully registered");
class FormDemo{
public static void main(String arg[]) {
try {
Form frame=new Form();
catch(Exception e) {}

May 1, 2009 at 4:45 PM


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 '' to show the success message.

Here is the code of '

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

class CreateForm extends JFrame{
setSize(400, 150);


Related Tutorials/Questions & Answers:
validation  validation
validation  we are doing payroll system project in java.So pls provide the complete validation code for employee master form
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 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
); JLabel label=new JLabel("JTable validation Example",JLabel.CENTER); JPanel panel=new JPanel(); panel.add(scroll); JFrame frame=new JFrame("JTable 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 - Struts
Validation  what is the best way to use validation in Struts?either "validation.xml" or 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
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:
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:
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