Developer can check if the given string is an alphabet,Number or etc
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; } }