Wednesday, February 27, 2013

Strict Alpha Numeric Validation

public class StrictAlphaNumericValidation {

    public static void main(String[] args) {
        System.out.println(isStrictAlphaNumeric("123abc"));
        System.out.println(isStrictAlphaNumeric("abc"));
        System.out.println(isStrictAlphaNumeric("123"));
        System.out.println(isStrictAlphaNumeric("abc123!@#"));
        System.out.println(isStrictAlphaNumeric("!@#abc123"));
    }

    public static boolean isStrictAlphaNumeric(String str) {
       if(str.matches("\\d{1,}[a-zA-Z]{1,}") || str.matches("[a-zA-Z]{1,}\\d{1,}")){
            return true;
        } else{
            return false;
        }
    }

}

Output:
true
false
false
false
false