Regrex_Expression.java
package regularExpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExpression {
public boolean Name(String st) {
boolean result = false;
String regex = "^[A-Z][a-z]{2,10}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean Postal(String st) {
boolean result = false;
String regex = "[0-9]{5}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean Gender(String st) {
boolean result = false;
if (st.equals("Male")||st.equals("Female")||st.equals("Other")) {
result = true;
} else {
result = false;
}
return result;
}
public boolean Phone(String st) {
boolean result = false;
String regex = "^[9]{1}[678]{1}[0-9]{8}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean CreditCard(String st) {
boolean result = false;
String regex = "[0-9]{4}[-]{1}[0-9]{4}[-]{1}[0-9]{4}[-]{1}[0-9]{4}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean CvcNumber(String st) {
boolean result = false;
String regex = "[0-9]{3}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean Email(String st) {
boolean result = false;
String regex = "^[a-z]{1}[a-z0-9_.]{5,20}[@]{1}[a-z]{5,10}[.]{1}[com]{2,3}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean UserName(String st) {
boolean result = false;
String regex = "^[A-Z]{1}[a-z]{2,10}[0-9\\_\\.]{1,20}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean Password(String st) {
boolean result = false;
String regex = "^[A-Za-z0-9@%$!#]{4,10}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
public boolean ConfirmPassword(String st) {
boolean result = false;
String regex = "^[A-Z]{1}[A-Za-z0-9]{6,10}[!@#$%^*._-]{1,5}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(st);
result = m.matches();
return result;
}
}
GUI
package Trial;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.toedter.calendar.JDateChooser;
import regularExpression.RegexExpression;
public class Demo {
JTextField nametxt, mobiletxt, emailtxt, addresstxt,
usernametxt, credittxt;
JComboBox titlebox, genderCombo;
JPasswordField passwordtxt;
JDateChooser DOBtxt;
JButton register;
public Demo() {
JFrame frame = new JFrame();
frame.setSize(950, 600);
frame.setLayout(new BorderLayout());
JPanel main = new JPanel();
main.setLayout(null);
frame.add(main,BorderLayout.CENTER);
JLabel customer = new JLabel("Fill Customer Details");
customer.setFont(new Font("Verdana", Font.PLAIN, 25));
customer.setBounds(300, 10, 400, 35);
main.add(customer);
JLabel titlelbl = new JLabel("Title: ");
titlelbl.setBounds(50, 120, 230, 25);
titlelbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(titlelbl);
String title1[] = { "Mr.", "Mrs.", "Miss." };
titlebox = new JComboBox(title1);
titlebox.setBounds(190, 120, 230, 30);
titlebox.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(titlebox);
JLabel Namelbl = new JLabel("Name: ");
Namelbl.setBounds(50, 180, 230, 25);
Namelbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Namelbl);
nametxt = new JTextField();
nametxt.setBounds(190, 180, 230, 30);
nametxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(nametxt);
JLabel Genderlbl = new JLabel("Gender: ");
Genderlbl.setBounds(50, 240, 230, 25);
Genderlbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Genderlbl);
String G1[] = { "Male", "Female", "Other" };
genderCombo = new JComboBox(G1);
genderCombo.setBounds(190, 240, 230, 30);
genderCombo.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(genderCombo);
JLabel DOBlbl = new JLabel("DOB: ");
DOBlbl.setBounds(50, 300, 230, 25);
DOBlbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(DOBlbl);
DOBtxt = new JDateChooser();
DOBtxt.setDateFormatString("yyyy-MM-dd");
DOBtxt.setBounds(190, 300, 230, 30);
DOBtxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(DOBtxt);
JLabel Mobilelbl = new JLabel("Mobile No: ");
Mobilelbl.setBounds(50, 360, 230, 25);
Mobilelbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Mobilelbl);
mobiletxt = new JTextField();
mobiletxt.setBounds(190, 360, 230, 30);
mobiletxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(mobiletxt);
JLabel Emaillbl = new JLabel("Email: ");
Emaillbl.setBounds(50, 420, 230, 25);
Emaillbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Emaillbl);
emailtxt = new JTextField();
emailtxt.setBounds(190, 420, 230, 30);
emailtxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(emailtxt);
JLabel Addresslbl = new JLabel("Address: ");
Addresslbl.setBounds(480, 120, 230, 25);
Addresslbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Addresslbl);
addresstxt = new JTextField();
addresstxt.setBounds(630, 120, 230, 30);
addresstxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(addresstxt);
JLabel Usernamelbl = new JLabel("Username: ");
Usernamelbl.setBounds(480, 180, 230, 25);
Usernamelbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Usernamelbl);
usernametxt = new JTextField();
usernametxt.setBounds(630, 180, 230, 30);
usernametxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(usernametxt);
JLabel Passwordlbl = new JLabel("Password: ");
Passwordlbl.setBounds(480, 240, 230, 25);
Passwordlbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Passwordlbl);
passwordtxt = new JPasswordField();
passwordtxt.setBounds(630, 240, 230, 30);
passwordtxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(passwordtxt);
JLabel Creditlbl = new JLabel("Credit No: ");
Creditlbl.setBounds(480, 300, 230, 25);
Creditlbl.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(Creditlbl);
credittxt = new JTextField();
credittxt.setBounds(630, 300, 230, 30);
credittxt.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(credittxt);
register = new JButton("Register");
register.setFocusable(false);
register.setBounds(230, 500, 130, 30);
register.setBackground(new Color(21, 159, 133));
register.setForeground(Color.WHITE);
register.setFont(new Font("Verdana", Font.PLAIN, 20));
main.add(register);
register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RegexExpression val = new RegexExpression();
if (e.getSource() == register) {
String Name = nametxt.getText();
boolean resultFName = val.Name(Name);
if (resultFName == true) {
String Gender = genderCombo.getSelectedItem().toString();
boolean Gender1 = val.Gender(Gender);
if (Gender1 == true) {
String number = mobiletxt.getText();
boolean result = val.Phone(number);
if (result == true) {
String email = emailtxt.getText();
boolean emailresult = val.Email(email);
if (emailresult == true) {
String username=usernametxt.getText();
boolean resultusername=val.UserName(username);
if(resultusername==true) {
String password=passwordtxt.getText();
boolean resultpassword=val.Password(password);
if(resultpassword==true) {
String credit=credittxt.getText();
boolean resultcredit=val.CreditCard(credit);
if(resultcredit==true) {
}
else {
JOptionPane.showMessageDialog(null, "Please enter your"
+ " proper credit card number! ");
}
}
else {
JOptionPane.showMessageDialog(null, "Please enter proper password!");
}
}
else {
JOptionPane.showMessageDialog(null, "Please enter proper Username!");
}
}
else {
JOptionPane.showMessageDialog(null, "Invalid Email");
}
}
else {
JOptionPane.showMessageDialog(null, "Invalid Mobile Number");
}
}
else {
JOptionPane.showMessageDialog(null, "Invalid Gender");
}
}
else {
JOptionPane.showMessageDialog(null, "Invalid Name");
}
}
}
});
frame.setVisible(true);
}
public static void main(String[] args) {
new Demo();
}
}
Output