Developer can check if the given string is an alphabet,Number or etc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; public class LoginFormValidations { private static Logger log = Logger.getLogger(LoginFormValidations. class ); private static Pattern numberPattern = Pattern.compile( "[0-9]+" ); private static Pattern alphabeticPattern = Pattern.compile( "[a-zA-Z]+" ); private static Pattern alphaNumericPattern = Pattern.compile( "[\x00-\x7F]+" ); /**Implementing method checks if the given string is a number or not * @param source * @return * */ public static boolean checkForNumbers(String source) { boolean flag= true ; try { Matcher numberMatcher = numberPattern.matcher(source); flag = numberMatcher.matches(); log.debug( "is Number flag=" +flag); } catch (Exception e){ log.debug( "exception in checkForNumbers" ); } return flag; } /**Implementing method checks if the given string is an alphabet or not * @param source * @return * */ public static boolean checkForAlphabet(String source) { boolean flag= true ; try { Matcher numberMatcher = alphabeticPattern.matcher(source); flag = numberMatcher.matches(); } catch (Exception e){ log.debug( "exception in checkForString" ); } return flag; } /**Implementing method checks if the given string is has ascii charecters or not * @param source * @return * */ public static boolean checkForAscii(String source) { boolean flag= true ; try { Matcher numberMatcher = alphaNumericPattern.matcher(source); flag = numberMatcher.matches(); log.debug( "Ascii=" +flag); } catch (Exception e){ log.debug( "exception in checkForString" ); } return flag; } /**Implementing method checks if the given string is of email format * @param source * @return * */ public static boolean checkForEmail(String source) { //Set the email pattern string Pattern p = Pattern.compile( ".+@.+\.[a-z]+" ); //Match the given string with the pattern Matcher m = p.matcher(source); //check whether match is found boolean matchFound = m.matches(); if (matchFound) log.debug( "Valid Email Id." ); else log.debug( "Invalid Email Id." ); return matchFound; } } |