Important alert: (current site time 7/15/2013 5:11:55 AM EDT)
 

VB icon

Validation

Email
Submitted on: 5/18/2013 11:39:56 AM
By: Vishwanet Computers Pvt. Ltd, Kolhapur 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.5)
Views: 336
 
     this application contains combination of key level validation and form level validation.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Validation
// Description:this application contains combination of key level validation and form level validation.
// By: Vishwanet Computers Pvt. Ltd, Kolhapur
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7139&lngWId=2//for details.//**************************************

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.util.regex.*;
public class FrmValidation extends JFrame implements ActionListener,KeyListener
{
	JTextField txtNm,txtEmailID,txtContactNo;
	JLabel lblNm,lblAdd,lblState,lblEmailID,lblContactNo,lblGender;
	JPanel jp;
	JTextArea txtAdd;
	JComboBox cmbState;
	JRadioButton rbtnMale,rbtnFemale;
	JButton btnDisp,btnClr;
	JFrame jf1;
FrmValidation()
{
	setSize(630,411);
	setLayout(null);
	setLocation(200,150);
	setName("Basic Validation Form...");
	Font f1=new Font("Verdana",1,10);
	
	jp=new JPanel();
	jp.setBounds(38,22,541,321);
	add(jp);
	jp.setBorder(BorderFactory.createEtchedBorder()); 
	jp.setLayout(null);
	lblNm=new JLabel("Name:");
	lblNm.setBounds(73,24,41,13);
	lblNm.setFont(f1);
	jp.add(lblNm);
	
	txtNm=new JTextField();
	txtNm.setFont(f1);
	txtNm.setBounds(125,21,262,20);
	txtNm.addKeyListener(this);
	jp.add(txtNm);
	
	lblAdd=new JLabel("Address:");
	lblAdd.setBounds(63,59,51,13);
	lblAdd.setFont(f1);
	jp.add(lblAdd);
	
	txtAdd =new JTextArea();
	txtAdd.setBounds(125,56,374,45);
	txtAdd.addKeyListener(this);
	txtAdd.setFont(f1);
	jp.add(txtAdd);
	
	lblState=new JLabel("State:");
	lblState.setBounds(61,124,38,13);
	lblState.setFont(f1);
	jp.add(lblState);
	
	txtEmailID=new JTextField();
	txtEmailID.setBounds(125,159,262,20);
	txtEmailID.addKeyListener(this);
	txtEmailID.setFont(f1);
	jp.add(txtEmailID);
		
		txtContactNo=new JTextField();
		txtContactNo.setBounds(125,195,135,20);
		txtContactNo.addKeyListener(this);
		txtContactNo.setFont(f1);
		txtContactNo.setColumns(10);
		jp.add(txtContactNo);
	cmbState=new JComboBox();
	cmbState.setBounds(125,121,192,21);
	cmbState.setFont(f1);
	cmbState.addItem("Plz select the State");
	cmbState.addItem("Karnataka");
	cmbState.addItem("Maharashtra");
	cmbState.addItem("Kerala");
	cmbState.addItem("West Bengaul");
	jp.add(cmbState);
	
	lblEmailID=new JLabel("Email ID:");
	lblEmailID.setBounds(61,163,53,12);
	jp.add(lblEmailID);
	lblEmailID.setFont(f1);
	
	lblContactNo=new JLabel("Contact No.:");
	lblContactNo.setBounds(44,199,70,13);
	jp.add(lblContactNo);
	lblContactNo.setFont(f1);
	
	lblGender=new JLabel("Gender:");
	lblGender.setBounds(66,231,48,13);
	lblGender.setFont(f1);
	jp.add(lblGender);
	
	btnDisp=new JButton("Display");
	btnDisp.setBounds(157,275,80,23);
	btnDisp.addActionListener(this);
	btnDisp.setFont(f1);
	jp.add(btnDisp);
	
	btnClr=new JButton("Clear");
	btnClr.setBounds(260,275,75,23);
	btnClr.addActionListener(this);
	btnClr.setFont(f1);
	jp.add(btnClr);
	
	rbtnFemale=new JRadioButton("Female");
	rbtnFemale.setBounds(125,231,70,17);
	rbtnFemale.setFont(f1);
	jp.add(rbtnFemale);
	
	rbtnMale=new JRadioButton("Male");
	rbtnMale.setBounds(191,231,55,17);
	rbtnMale.setFont(f1);
	jp.add(rbtnMale);
	
	setVisible(true);	
}
public void actionPerformed(ActionEvent e)
{
	if(e.getSource()==btnClr)
	{
		txtNm.setText(" ");
		txtAdd.setText(" ");
		txtEmailID.setText(" ");
		txtContactNo.setText(" ");
		cmbState.setSelectedIndex(0);
		txtNm.requestFocus(true);	
	}
	if(e.getSource()==btnDisp)
	{
		if(txtNm.getText()==""|| txtAdd.getText()==""||txtContactNo.getText()==""||txtEmailID.getText()==""||cmbState.getSelectedIndex()==0||(rbtnFemale.isSelected()==false&& rbtnMale.isSelected()==false))
		{
			JOptionPane.showMessageDialog(jf1,"plz fill all blank fields");
			return;
		}
		
		Pattern p=Pattern.compile("^[_a-z0-9-]{4,15}+@[a-z]{3,8}+.[a-z]{2,4}$");
		Matcher m=p.matcher(txtEmailID.getText());
		if(m.matches()==false)
		{
			JOptionPane.showMessageDialog(jf1,"enter valid email ID");
						txtEmailID.requestFocus();
			return;
		}	
		p=Pattern.compile("^[0-9]{10}$");
		m=p.matcher(txtContactNo.getText());
		if(m.matches()==false)
		{
			JOptionPane.showMessageDialog(jf1,"enter valid Contact number");
			txtContactNo.requestFocus();
			return;
		} 
		String s="=====================================\nName :"+txtNm.getText();
		s=s+"\nAddress :"+txtAdd.getText()+"\nState :"+cmbState.getSelectedItem();
		s=s+"\nEmail ID :"+txtEmailID.getText()+"\nContact No. :"+txtContactNo.getText();
		if(rbtnFemale.isSelected()==true)
			 	s=s+"\nGender :"+rbtnFemale.getText();
		else
			s=s+"\nGender :"+rbtnMale.getText();
		s+="\n=============================================";
		JOptionPane.showMessageDialog(null,s);
			
	}
}
public void keyReleased(KeyEvent ke1)
	{
		
	}
	public void keyPressed(KeyEvent ke2)
	{
		
	}
	public void keyTyped(KeyEvent ke3)
	{
		if(ke3.getSource()==txtNm)
		{
					if((ke3.getKeyChar()>='A'&&ke3.getKeyChar()<='Z')||(ke3.getKeyChar()>='a'&&ke3.getKeyChar()<='z')||ke3.getKeyChar()==KeyEvent.VK_BACK_SPACE||ke3.getKeyChar()==KeyEvent.VK_SPACE)
					{
											
					}
					else
					{
						JOptionPane.showMessageDialog(jf1,"Not allowed other symbols");
						ke3.consume();
					}
		}
		if(ke3.getSource()==txtAdd)
		{
			if((ke3.getKeyChar()>='A'&&ke3.getKeyChar()<='Z')||(ke3.getKeyChar()>='a'&&ke3.getKeyChar()<='z')|| (ke3.getKeyChar()>='0'&&ke3.getKeyChar()<='9')|| ke3.getKeyChar()==KeyEvent.VK_BACK_SPACE||ke3.getKeyChar()==KeyEvent.VK_SPACE||ke3.getKeyChar()=='-'||ke3.getKeyChar()==','||ke3.getKeyChar()=='/')
			{
											
			}
			else
			{
					JOptionPane.showMessageDialog(jf1,"Not allowed other symbols");
					ke3.consume();
			}
			
			
		}
		if(ke3.getSource()==txtContactNo)
		{
			if(txtContactNo.getText().length()>9)
			{
				JOptionPane.showMessageDialog(jf1,"Plz enter only 10 numbers");
				ke3.consume();
				return;
			}
			if((ke3.getKeyChar()>='0'&&ke3.getKeyChar()<='9')|| ke3.getKeyChar()==KeyEvent.VK_BACK_SPACE)
			{
							
			}
			else
			{
					JOptionPane.showMessageDialog(jf1,"Plz enter only numbers");
					ke3.consume();
			}
		}
		
		
		
	}
public static void main(String[] args)
{
	new FrmValidation();
}

}


Other 16 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.