Validation in Java
Validations:
1. If word contains consecutive characters
String userWord = "Pankaj";
Pattern pattern = Pattern.compile("(\\w)\\1{2,}");
Matcher matcher = pattern.matcher(userWord);
if (matcher.find()) {
System.out.println("Found value is: " + matcher.group(0));
return true;
} else {
System.out.println("NO MATCH");
return false;
}
2. If word contains more instance of characters
String userWord = "Pankaj";
Pattern pattern = Pattern.compile("(\\w)(\\w*\\1){4}");
Matcher matcher = pattern.matcher(userWord);
if (matcher.find()) {
System.out.println("Found value is: " + matcher.group(0));
return true;
} else {
System.out.println("NO MATCH");
return false;
}
3. If word contains any name
String firstName = "Pankaj";
String emailAddress = "pankaj.lilhare@gmail.com";
// Here I want to check first name is exist on given emailAddress.
return (firstName.toUpperCase().matches("(.*)"+emailAddress.toUpperCase()+"(.*)")) ? true :false;
4. To check name should not contains Consecutive special characters like - _ . and space
Pattern pattern = Pattern.compile("[-_. ][-_. ]");
5. To check name should not start with special characters
Pattern pattern = Pattern.compile("^[?<>~!@#$%^&()`,.{}|;:*'\\[\\]\\-_=+\"\\/\\\\ ]");
6. To check name should not ends with special characters
Pattern pattern = Pattern.compile("[?<>~!@#$%^&()`,.{}|;:*'\\[\\]\\-_=+\"\\/\\\\ ]$");
Comments
Post a Comment