UNKNOWN //************************************** // Name: Validation // Description:this application contains combination of key level validation and form level validation. // By: Vishwanet Computers Pvt. Ltd, Kolhapur // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.7139/lngWId.2/qx/vb/scripts/ShowCode.htm //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(); } }