Jun 12, 2011

Using REGEX in Validations

Recently I saw many questions on Discussion Boards regarding validations. Folks always want to go with validations avoiding Apex.


We can not ignore flexibility provided by Apex but I always prefer Validation when I can implement my business logics with it. I am bit lazy kind of person and hate writing test methods, that's why I use validation because if there is no code then no test method. Thanks to Salesforce for providing these kind of native functionalities where I do not have to write any code.


I am going bit off track as there is no point discussing over this. So coming back to REGEX in Validations, I have some good examples.


Example 1 : Validate Credit Card Number
Description :
Validates that a custom text field called Credit_Card_Number is formatted in 9999-9999-9999-9999 or 9999999999999999 number format when it is not blank.The pattern specifies:
• Four digits (0-9) followed by a dash: \\d{4}-
• The aforementioned pattern is repeated three times by wrapping it in () {3}
• Four digits (0-9)
• The OR character (|) allows an alternative pattern of 16 digits of zero through nine with no dashes: \\d{16}


Validation Formula :
NOT( REGEX( Credit_Card_Number__c ,
"(((\\d{4}-){3}\\d{4})|\\d{16})?"))
Error Message : Credit Card Number must be in this format: 9999-9999-9999-9999 or 9999999999999999.


Example 2 : Valid IP Address
Description :
Ensures that a custom field called IP Address is in the correct format, four 3-digit numbers (0-255) separated  by periods.

Validation Formula :
NOT(
REGEX( IP_Address__c,
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
))
Error Message : IP Address must be in form 999.999.999.999 where each part is between 0 and 255.


Example 3 : US Phone Number Has Ten Digits
Description :
Validates that the Phone number is in (999) 999-9999 format. This works by using the

REGEX function to check that the number has ten digits in the (999) 999-9999 format.


Validation Formula : 
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
Error Message : US phone numbers should be in this format: (999) 999-9999.


Example 4 : Social Security Number Format
Description :
Validates that a custom text field called SSN is formatted in 999-99-9999 number format

(if it is not blank). The pattern specifies:
• Three single digits (0-9):\\d{3}
• A dash
• Two single digits (0-9):\\d{2}
• A dash
• Four single digits (0-9):\\d{4}


Validation Formula :
NOT(
OR(
LEN (Social_Security_Number__c) = 0,
REGEX( Social_Security_Number__c , "[0-9]{3}-[0-9]{2}-[0-9]{4}")
))
Error Message : SSN must be in this format: 999-99-9999.


This will provide am idea how we can write validations using REGEX.


Cheers.



20 comments:

  1. Great Ankit....you have given valuable information...

    ReplyDelete
  2. am write vr
    NOT(REGEX(Phone_c, "[0-9]{10}")) for mobileno it is not working why

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi Ankit,

    I want to compare First character of Lead Last name in Workflow rule. Suppose it is between A to L, then one Task will be created and if it is between M to Z then another tasks will be created.
    For that I have written workflow rule as below:
    REGEX(LEFT( LastName ,1), "[a-lA-L] {1}")

    but it gives me an error : "Function REGEX may not be used in this type of formula".

    Can you please suggest?

    ReplyDelete
    Replies
    1. Sounds like you need this, let me know if works.
      REGEX( LastName, "[a-lA-L]{1}")

      Delete
  5. hey ankit can u pls provide validation for email format.


    ReplyDelete
  6. NOT
    ( AND
    (
    ISBLANK( Skills_Required__c ),
    REGEX( Skills_Required__c , "[a-zA-Z]")
    )
    )

    I do not want any numbers just only text but the above formula is not working please help me with the correct one.

    ReplyDelete
  7. I want check or accept name in the following format.

    "123 Test User", "Test User 123"

    In this case each word starting letter caps and other letter or small. In starting numbers are also accepted and all special characters also accepted. If anyone know reply.

    ReplyDelete
  8. Hello - thank you for posting this, as some of it is very helpful, but if I may I have to question how well your regex for phone numbers works. It matches a phone number, but does not exclude bizarre inputs like: "1.2.3.4.5.6.7.8.9.0" and "asdf;lkj1234567890" simply it is too permissive.

    May I instead suggest something like: !REGEX(Phone, '\\(?\\d{3}\\)?[-\\.\\s]?\\d{3}[-\\.\\s]?\\d{4}')

    Which allows 0 or 1 opening parenthesis, requires 3 digits, allows 0 or 1 close parenthesis, allows a dash or dot or space, requires 3 digits, allows a dash or dot or space, then requires 4 digits. Something even tighter could be written, because this allows "(123-456.7890", but this is considerably less tolerant than the one you provide, and I would consider it to be the loosest regex for phone numbers acceptable.

    ReplyDelete
  9. Can I use REGEX in a formula field (not Validation) to populate a checkbox if a TEXT field containing an email address is not a valid email address?

    ReplyDelete
  10. Can we solve 99-999-99 this without using REGEX

    ReplyDelete
  11. Hi Ankit.
    I want to write validation rule for indian mobile number and that number should be start 7 series. How to write this VR pls help me

    ReplyDelete
  12. Hi Ankit, is there any REGEX to find the Credit card with With spaces like 4444 5555 6666 7777 . Please suggest.

    ReplyDelete
  13. Its so easy and comfort to use this REGEX. We should follow this step and and apply it. For more, you can also take advantages to get the Apartment Cleaning Service in Phoenix AZ. These service need to secure your bags and luggage.

    ReplyDelete