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.
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();
if (isEmployeeIDValid(employeeID)) {
System.out.println("Employee ID is valid");
} else {
System.out.println("Employee ID 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();
}
}
Comments
Leave a comment