Apex Regex Validation Factory

Validation Factory

public with sharing class Regex_Validator {

    public static Boolean isValidEmail(String emailAddress) {
        String emailRegex = '^[a-zA-Z0-9._}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$';
        return String.isBlank(emailAddress) ? false : Pattern.compile(emailRegex).matcher(emailAddress).matches();
    }

    public static Boolean isValidPassword(String password){
        String passwordRegex = '^.*(?=.{8,})(?=.*\\d)(?=.*[a-zA-Z])|(?=.{8,})(?=.*\\d)(?=.*[!@#$%^&])|(?=.{8,})(?=.*[a-zA-Z])(?=.*[!@#$%^&]).*$';
        return String.isBlank(password) ? false : Pattern.compile(passwordRegex).matcher(password).matches();
    }

    public static Boolean isValidDate(String dateString){
        String dateRegex = '^\\d{4}-\\d{2}-\\d{2}$';
        return String.isBlank(dateString) ? false : Pattern.compile(dateRegex).matcher(dateString).matches();
    }

    public static Boolean isValidSSN(String ssn){
        String ssnRegex = '[0-9]{3}[-]?[0-9]{2}[-]?[0-9]{4}';
        return String.isBlank(ssn) ? false : Pattern.compile(ssnRegex).matcher(ssn).matches();
    }

    public static Boolean isValidPhone(String phone){
        String phoneRegex='^([0-9\\(\\)\\/\\+ \\-]*)$';
        return String.isBlank(phone) ? false : Pattern.compile(phoneRegex).matcher(phone).matches();
    }

    public static Boolean isValidZipCode(String zipCode){
        String zipRegex='^\\d{5}(?:[-\\s]?\\d{4})?$';
        return String.isBlank(zipCode) ? false : Pattern.compile(zipRegex).matcher(zipCode).matches();
    }

    public static Boolean isValidNumber(String num) {
         String numberRegex = '^[0-9]\\d*(\\.\\d+)?$';
     return String.isBlank(num) ? false : Pattern.compile(numberRegex).matcher(num).matches();
    }

    public static Boolean isValidSfId(String id){
       String idRegex = '[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}';
       return String.isBlank(id) ? false : Pattern.compile(idRegex).matcher(id).matches();
    }
}

Validation Factory Test Class

@isTest
private class Regex_Validator_Test {

    public static User setupUser(){
        User user = (User)SmartFactory.createSObject('User');
        return user;
    }

    @isTest static void test_isValidEmail() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidEmail('test@mail.com'),true);
            System.assertEquals(Regex_Validator.isValidEmail('tmichels+5@mygmail.com'),true);
            System.assertEquals(Regex_Validator.isValidEmail('testmail.com'),false);
            System.assertEquals(Regex_Validator.isValidEmail('tes13_?//@tmail.com'),false);
            System.assertEquals(Regex_Validator.isValidEmail('testmail'),false);
            System.assertEquals(Regex_Validator.isValidEmail('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidEmail(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidPassword() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidPassword('Password123!'),true);
            System.assertEquals(Regex_Validator.isValidPassword('password123!'),true);
            System.assertEquals(Regex_Validator.isValidPassword('password!'),true);
            System.assertEquals(Regex_Validator.isValidPassword('Password123'),false);
            System.assertEquals(Regex_Validator.isValidPassword('blah'),false);
            System.assertEquals(Regex_Validator.isValidPassword('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidPassword('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidPassword(null),false);
      test.stopTest();
    }

    @isTest static void test_isValidDate() {
        test.startTest();
                System.assertEquals(Regex_Validator.isValidDate('01/01/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('1/1/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('01-01-2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('2016-01-01'),true);
                System.assertEquals(Regex_Validator.isValidDate('01.01.2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('1.1.2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('33/01/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('01/32/2016'),false);
                System.assertEquals(Regex_Validator.isValidDate('01/01/3016'),false);
                System.assertEquals(Regex_Validator.isValidDate('adasdsad'),false);
                System.assertEquals(Regex_Validator.isValidDate('!@#$%%^&*()'),false);
                System.assertEquals(Regex_Validator.isValidDate(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidSSN() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidSSN('222-22-2222'),true);
            System.assertEquals(Regex_Validator.isValidSSN('222-2-2222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('22-22-2222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('22-22-222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('222222222'),true);
            System.assertEquals(Regex_Validator.isValidSSN('2222222'),false);
            System.assertEquals(Regex_Validator.isValidSSN('asdsad'),false);
            System.assertEquals(Regex_Validator.isValidSSN('!@#$%%^&*()'),false);
            System.assertEquals(Regex_Validator.isValidSSN(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidPhone() {
        test.startTest();
                System.assertEquals(Regex_Validator.isValidPhone('(408)421-3722'),true);
                System.assertEquals(Regex_Validator.isValidPhone('4084213722'),true);
                System.assertEquals(Regex_Validator.isValidPhone('+1(408)421-3722'),true);
                System.assertEquals(Regex_Validator.isValidPhone('(333)222-999'),true);
                System.assertEquals(Regex_Validator.isValidPhone('3332229999'),true);
                System.assertEquals(Regex_Validator.isValidPhone('asdsadsad'),false);
                System.assertEquals(Regex_Validator.isValidPhone('!@#$$%%@'),false);
                System.assertEquals(Regex_Validator.isValidPhone(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidZipCode() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidZipCode('95070'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('95070-2344'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('95070-234'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('950702344'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('99999'),true);
            System.assertEquals(Regex_Validator.isValidZipCode('999'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('9a9n9'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('asdasd'),false);
            System.assertEquals(Regex_Validator.isValidZipCode('!@#$$%%@'),false);
            System.assertEquals(Regex_Validator.isValidZipCode(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidNumber() {
        test.startTest();
            System.assertEquals(Regex_Validator.isValidNumber('95070'),true);
            System.assertEquals(Regex_Validator.isValidNumber('-99'),false);
            System.assertEquals(Regex_Validator.isValidNumber('asdasd'),false);
            System.assertEquals(Regex_Validator.isValidNumber('!@#$$%%@'),false);
            System.assertEquals(Regex_Validator.isValidNumber(null),false);
        test.stopTest();
    }

    @isTest static void test_isValidSfId(){
      test.startTest();
          System.assertEquals(Regex_Validator.isValidSfId('32423324'),false);
          System.assertEquals(Regex_Validator.isValidSfId('00363000005eIbz'),true);
          System.assertEquals(Regex_Validator.isValidSfId('00363000005eIbzAlz'),true);
          System.assertEquals(Regex_Validator.isValidSfId(null),false);
      test.stopTest();
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: