Utility methods which include validations for Zipcode ,SSN ,creditcard as per US conventions email,and Alpha-Numeric check
import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Hashtable; import java.util.Locale; import java.util.Properties; import java.util.StringTokenizer; import org.apache.log4j.Logger; public class Util { /** * Checks the given zip code to see if it adhears * the 5-4 format (with no hyphen) * * @param zip code to check * @return true if proper format, false otherwise. */ public static boolean isZipValidNoHyphen(String zip) { final String valid = "0123456789"; //check for null if (zip==null) { return false; } // first check length: must be 5 or 9 if (zip.length() != 5 && zip.length() != 9) { return false; } // iterate thru string checking each char is valid for (int i=0; i < zip.length(); i++) { String temp = zip.substring(i, i+1); // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } } return true; } /** * Checks the given zip code to see if it adhears * the 5-4 format (with hyphen). * * @param zip code to check * @return true if proper format, false otherwise. */ public static boolean isZipValid(String zip) { final String valid = "0123456789-"; int hyphencount = 0; //check for null if (zip==null) { return false; } // first check length: must be 5 or 10 if (zip.length() != 5 && zip.length() != 10) { return false; } // if the length is 10, make sure a hypen occures // at the proper location if ( (zip.length() == 10) && (zip.charAt(5) != '-') ) { return false; } // iterate thru string checking each char is valid for (int i=0; i < zip.length(); i++) { String temp = zip.substring(i, i+1); if (temp.equals("-")) hyphencount++; // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } // make sure there is only one hypen possible if (hyphencount > 1) { return false; } } return true; } /** * Checks the given zip code to see if it * is 5 numeric digits. * * @param zip code to check * @return true if proper format, false otherwise. */ public static boolean isZipFiveDigits(String zip) { final String valid = "0123456789"; //check for null if (zip==null) { return false; } // first check length: must be 5 if (zip.length() != 5) { return false; } // iterate thru string checking each char is valid for (int i=0; i < zip.length(); i++) { String temp = zip.substring(i, i+1); // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } } return true; } /** * Checks to see if zipPlusFour is 4 numeric digits * * @param zipPlusFour to check * @return true if proper format, false otherwise. */ public static boolean isZipPlusFourValid(String zipPlusFour) { final String valid = "0123456789"; //check for null if (zipPlusFour==null) { return false; } // first check length: must be 4 if (zipPlusFour.length() != 4) { return false; } // iterate thru string checking each char is valid for (int i=0; i < zipPlusFour.length(); i++) { String temp = zipPlusFour.substring(i, i+1); // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } } return true; } /** * Checks to see if SSN is 9 numeric digits * * @param ssn to check * @return true if proper format, false otherwise. */ public static boolean isSSNValid(String ssn) { final String valid = "0123456789"; //check for null if (ssn==null) { return false; } // first check length: must be 9 if (ssn.length() != 9) { return false; } // iterate thru string checking each char is valid for (int i=0; i < ssn.length(); i++) { String temp = ssn.substring(i, i+1); // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } } return true; } /** checks for a valid email * * @param an email address * @return false for invalid addresses, true otherwise * */ public static boolean isValidEmail(String email) { //check for null if (email==null) { return false; } // check the length if (email.length() < 5) { // a@b.c should be the shortest an // address could be return false; } // check format // email must have at least one "@" if (email.indexOf('@') == -1) { return false; } // email must have at least one "." if (email.indexOf('.') == -1) { return false; } // make sure email has at least 1 if ( //((email.length() - (email.lastIndexOf('.') + 1)) > 3) || ((email.length() - (email.lastIndexOf('.') + 1)) < 1) ) { return false; } // make sure there is no "_" after the "@" if ( (email.indexOf('_') != -1) && (email.lastIndexOf('_') > email.lastIndexOf('@')) ) { return false; } // make sure there is only one "@" // and each character is valid [a-z, 0-9, @, ., -, _] int atCnt = 0; for (int i=0; i < email.length(); i++) { char temp = email.charAt(i); if (temp == '@') atCnt++; // make sure there is only one @ possible if (atCnt > 1) { return false; } // make sure the current char is part of valid chars if ( (!isDigit(temp)) && (!isAlpha(temp)) && (temp != '@') && (temp != '.') && (temp != '-') && (temp != '_') ) { return false; } } // at this point we passed all checks return true; } /** * Method isCreditCardValid * Checks if the given credit card number is valid. * * @param cc * @return boolean */ public static boolean isCreditCardValid(String cc) { final String valid = "0123456789"; int sum = 0; int digit = 0; int addend = 0; boolean timesTwo = false; //check for null if (cc==null) { return false; } // first check length: must be 13-16 digits if (cc.length() < 13 || cc.length() > 16) { return false; } // iterate thru string checking each char is valid (numeric) for (int i=0; i < cc.length(); i++) { String temp = cc.substring(i, i+1); // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } } // perform Luhn check for credit card number validity for (int i = cc.length () - 1; i >= 0; i--) { digit = Integer.parseInt (cc.substring (i, i + 1)); if (timesTwo) { addend = digit * 2; if (addend > 9) { addend -= 9; } } else { addend = digit; } sum += addend; timesTwo = !timesTwo; } int modulus = sum % 10; if (modulus != 0) { return false; } return true; } /** * Checks if a given string is valid for credit info * * @param string to check * @return true if valid, false otherwise */ public static boolean isValidForCreditInfo(String aName) { String temp; char tempChar; //check for null if (aName==null) { return false; } // iterate thru string checking each char is valid for (int i=0; i < aName.length(); i++) { temp = aName.substring(i, i+1); tempChar = temp.charAt(0); // make sure the current char is part of valid chars for credit info if ( (!isDigit(tempChar)) && (!isAlpha(tempChar)) && (tempChar != ' ') && (tempChar != '&') && (tempChar != '-') && (tempChar != '\'') ) { return false; } } return true; } /** * Checks the given string to see if it is numeric * * @param string to check * @return true if proper format, false otherwise. */ public static boolean isNumeric(String aString) { final String valid = "0123456789"; //check for null if (aString==null) { return false; } // iterate thru string checking each char is valid for (int i=0; i < aString.length(); i++) { String temp = aString.substring(i, i+1); // make sure the current char is part of valid chars if (valid.indexOf(temp) == -1) { return false; } } return true; } /** * Checks if the given character is a digit. * * @param char to check * @return true if char is 0 - 9, false otherwise. */ public static boolean isDigit(char ch) { if ( (Character.toLowerCase(ch) >= '0') && (Character.toLowerCase(ch) <= '9') ) { return true; } else { return false; } } /** * Checks if the given character is an alpha. * * @param char to check * @return true if char is A-Z or a-z, false otherwise. */ public static boolean isAlpha(char ch) { if ( (Character.toLowerCase(ch) >= 'a') && (Character.toLowerCase(ch) <= 'z') ) { return true; } else { return false; } } }