Phone Number Format Validation Rules in Salesforce

Recently I had to create validation rules for two different phone number fields: home phone number and mobile phone number. These have different formats. With some research and mentoring from my father Michael I believe that I came up with the ideal solution and will share it with you now. It should be noted that these formats apply to the Australian locale. However, if the concepts are understood then a validation rule for any other locale’s phone number format should be easy enough. Feel free to skip to the end if you just want the formulas without the explanation.

There’s a few different ideas involved here which I’ll try to explain as succinctly as possible:

  1. Remember that validation rule formulas show the criteria for an error message. Therefore you have to think unintuitively; in reverse. You want the formula to evaluate to everything that you don’t want being true, so that everything else triggers the rule and won’t allow the record to be saved. Confused enough? Let’s continue.
  2. You want the record to be saved if (a) it adheres to the correct format, or (b) there is no number entered at all. This duality of alternative acceptable outcomes is achieved formulaically using an IF statement, with two different components. Here is the format of an IF statement: IF(logical_test, value_if_true, value_if_false).
  3. Our logical test will be the check if the phone number field is blank. This is achieved using the ISBLANK function, as follows: ISBLANK( HomePhone ).
  4. If it is blank then do we want the whole formula to evaluate to true or false? If it was blank and it was true that would make the validation trigger when it was blank and we don’t want that. So the answer is false, we want it to be false. Here’s what we have so far: IF(ISBLANK(HomePhone), false.
  5. Now we’ll add the final part of our IF statement. Our goal is for it to make the validation rule evaluate to false if the phone number adheres to the desired format, and thereby not trigger the validation rule. So how do we do that? There’s actually two functions involved: NOT and REGEX.
  6. I’ll explain REGEX first. REGEX means regular expression. For our purposes REGEX is used to define exactly what a string of characters must be like. It has a syntax not very accessible to the layperson but is easy enough to learn situationally. Here’s what it will look like: REGEX(  HomePhone  , ‘\\(0[0-9]{1}\\)[ ][0-9]{4}[ ][0-9]{4}’)
  7. The function NOT is used because we want this part of the IF statement (and therefore the whole formula) to evaluate to false (if the field isn’t blank) and therefore not trigger the rule so the record can save. Here’s what the whole thing will look like: IF(
    ISBLANK( HomePhone ) , false , NOT( REGEX( HomePhone , ‘\\(0[0-9]{1}\\)[ ][0-9]{4}[ ][0-9]{4}’))
    )

So let’s consider there are two scenarios: a phone number meets the REGEX rule and a phone number does not meet the REGEX rule. If it does then REGEX( , ) = true. If it doesn’t then REGEX ( , ) = false. Remember it’s embedded in the NOT function though, what does this mean? It makes it negative. If the phone number is in the right format then the REGEX evaluates the true and in turn the NOT evaluates to false because it’s looking for something that isn’t true in order to evaluate to true. If the phone number doesn’t meet the format then REGEX evaluates to false. NOT switches this around to true, making the whole formula evaluate to true and therefore the validation rule is triggered. Remember for our purposes the validation rule must evaluate to false to not be triggered. Or perhaps it just rejects every other possible outcome, whichever way you prefer to think about it!

On to the formulas…

Firstly, home phone numbers. In Australia the format is as follows: (00) 0000 0000. The first two digits within the number are the area code, and are associated with the state (I think, but may be wrong). Here’s the format in Salesforce REGEX syntax:

REGEX(  HomePhone  , ‘\\(0[0-9]{1}\\)[ ][0-9]{4}[ ][0-9]{4}’)

  • HomePhone = the field in question
  • ‘ = begin expression
  • \\ = make the next character not activate some other function, take it literally
  • ( = parenthesis for area code
  • 0 = always the first number of area code
  • [0-9] = indicates the values accepted in this next part
  • {1} = indicates that only one character (of the types [0-9]) will be accepted
  • \\) = add another bracket
  • [ ] = add a space
  • [0-9] = add more characters but only 0 – 9 numbers acceptable
  • {4} = 4 characters accepted
  • [ ] = add another space
  • [0-9] = add more characters, only numbers 0 – 9
  • {4} = 4 characters accepted
  • ‘ = end expression

Now we’ll add this REGEX expression to the larger formula:

IF(
ISBLANK( HomePhone ) , false , NOT( REGEX( HomePhone , ‘\\(0[0-9]{1}\\)[ ][0-9]{4}[ ][0-9]{4}’))
)

Now, Mobile Phone Numbers. The format is: 0000 000 000. Here’s the REGEX formula:

REGEX( MobilePhone , ‘[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}’ )

Here’s the REGEX expression embedded into the larger formula:

IF(
ISBLANK( MobilePhone ) , false,   NOT( REGEX(  MobilePhone  , ‘[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}’ ) )
)

Finally I’ll note that these validation rules should be added to the Contact object. This is achieved in Lightning by going to Setup > Objects and Fields > Object Manager > Contact.

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