Write a Java program to check the validity of a given employee ID using RegEx.
An employee ID is valid if and only if consists of 8 to 10 characters in which the first two characters is 01 or 02 or 03 followed by 4-6 alphabetic characters followed by 2 numeric digits.
Improve your previous program to also check the validity of a given employee’s email using RegEx. An employee’s email is considered valid if and only if has the following format: Name.surname@company.nyc.com.
Each of Name and surname consists of 4 to 8 alphabetic characters.
Add a validating method that checks the validity of a given employee’s password using RegEx. An employee’s password is considered valid if and only if consists of 12 word characters, starts with an underscore, ends with “@nyc” and contains one digit.
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter employee ID: ");
String employeeID = keyBoard.nextLine();
System.out.print("Enter employee email: ");
String employeeEmail = keyBoard.nextLine();
System.out.print("Enter employee password: ");
String password = keyBoard.nextLine();
if (isEmployeeIDValid(employeeID)) {
System.out.println("Employee ID is valid");
} else {
System.out.println("Employee ID is NOT valid");
}
if (isEmployeeEmailValid(employeeEmail)) {
System.out.println("Employee email is valid");
} else {
System.out.println("Employee email is NOT valid");
}
if (isPasswordValid(password)) {
System.out.println("Employee password is valid");
} else {
System.out.println("Employee password is NOT valid");
}
keyBoard.close();
}
/**
* An employee ID is valid if and only if consists of 8 to 10 characters in
* which the first two characters is 01 or 02 or 03 followed by 4-6 alphabetic
* characters followed by 2 numeric digits.
*
* @param employeeID
* @return
*/
private static boolean isEmployeeIDValid(String employeeID) {
if (employeeID.length() < 8 || employeeID.length() > 10) {
return false;
}
final String ID_REGEX = "^0" + "[0-3]" + "[a-zA-Z]{4,6}" + "[0-9]" + "[0-9]";
Pattern pattern = Pattern.compile(ID_REGEX);
Matcher matcher = pattern.matcher(employeeID);
return matcher.matches();
}
/***
* check the validity of a given employee’s email using RegEx. An employee’s
* email is considered valid if and only if has the following format:
* Name.surname@company.nyc.com.
*
* Each of Name and surname consists of 4 to 8 alphabetic characters.
*
* @param employeeEmail
* @return
*/
private static boolean isEmployeeEmailValid(String employeeEmail) {
final String EMAIL_REGEX = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@"
+ "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_REGEX);
Matcher matcher = pattern.matcher(employeeEmail);
return matcher.matches();
}
/***
* Add a validating method that checks the validity of a given employee’s
* password using RegEx. An employee’s password is considered valid if and only
* if consists of 12 word characters, starts with an underscore, ends with
* “@nyc” and contains one digit.
*
* @param password
* @return
*/
private static boolean isPasswordValid(String password) {
if (password.length() != 12) {
return false;
}
final String PASSWORD_REGEX = "^_" + "[a-zA-Z0-9\\s]" + "+@nyc$";
Pattern pattern = Pattern.compile(PASSWORD_REGEX);
Matcher matcher = pattern.matcher(password);
return matcher.matches();
}
}
Comments
Leave a comment