Friday, September 21, 2012

SSN Validator using matches()



SSNValidatorApp : You can use this to validate a textfield. Here it is SSN. Or see below for an easier method to validate.
public class SSNValidatorApp{
    public static void main(String[] args)    {
        boolean isValid =  false;
        while(!isValid)        {
        Scanner sc = new Scanner(System.in);    
        isValid = Validator.getSSN(sc, "Enter a Social Security Number: ");
        if(isValid)
            System.out.println("It is a valid SSN. Thank you");
        else
            System.out.println("It is an invalid SSN. Please try again");
        }    }  
public static boolean getSSN(Scanner sc, String prompt)    {
        System.out.print(prompt);
        String ssn = sc.nextLine();
        boolean isNumeric = false;       
            if(ssn.length() == 11)            {               
                if(getNumeric(ssn.substring(0, 3)))                {
                    if(ssn.charAt(3) == '-')                    {
                        if(getNumeric(ssn.substring(4, 5)))                        {
                            if(ssn.charAt(6) == '-')                            {
                                if(getNumeric(ssn.substring(7, 11)))                                {
                                    isNumeric = true;
                                }          }           }            }                }            }   
        return isNumeric;        }
   
    public static boolean getNumeric(String subString)    {
        boolean isNumeric = false;
        int i = 0;        
        while(i <= (subString.length()-1))        {
            char subStringLetter = subString.charAt(i);          
            switch(subStringLetter)           {
                case('0'):                  
                    isNumeric = true;
                    break;
                ………….. //put all cases  from 0 to 9 here
                case('9'):
                    isNumeric = true;
                    break;
                default:
                    isNumeric = false;                    
            }
            if(isNumeric)
                i++;
            else
                break;
        }
        return isNumeric;      
      }}}
Here is the easier way of doing it. Use matches() in Regular Expression provided by Java API. You can include the java documents in eclipse by clicking :    windows> show view> java docs if you want to see the description of the method you are using
public class SSNValidator {
        public static void main(String[] args) {
                SSNValidator ssnValidator = new SSNValidator();
                String[] ssnList = { null, "", "909-90-9900", "3423-343-23423",
"312fsdfas-sdfsaf-sadfas", "ada-dd-dasd",
                                "123-32-4532", "123*32*4532", "\t" };
                for (String ssn : ssnList) {
                        System.out.println("SSN Value [" + ssn + "] is " +
(ssnValidator.isValid(ssn) ? "valid" : "invalid") + ".");
                }
        }

        public boolean isValid(String ssn) {
                return ssn != null && ssn.matches("\\d{3}-\\d{2}-\\d{4}");

        }
}

No comments:

Post a Comment