Ask Search:
Andrine ClarkeAndrine Clarke 

Contact phone numbers validation rule

How do I create one validation rule to ensure that all phone/fax/mobile numbers are in the correct format? I don't want to have to write a separate validation rule for each of the following fields:

Phone
Mobile
Fax
Home Phone
Other Phone

Thanks in advance for the help.
Best Answer chosen by Moderator (salesforce.com) 
Andrine ClarkeAndrine Clarke
I figured it out. Thanks to Force Guru at http://forceguru.blogspot.com/2011/05/validation-over-apex.html. Here's the validation rule, I ended up using.

OR(
IF(
ISBLANK(Phone) , false , NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(MobilePhone) , false , NOT(REGEX(MobilePhone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(Fax) , false , NOT(REGEX(Fax, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(HomePhone) , false , NOT(REGEX(HomePhone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(OtherPhone) , false , NOT(REGEX(OtherPhone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(AssistantPhone) , false , NOT(REGEX(AssistantPhone, "\\D*?(\\d\\D*?){10}")))
)

All Answers

Erik SchleichErik Schleich
This should work;

AND(
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}")),
NOT(REGEX(MobilePhone, "\\D*?(\\d\\D*?){10}")),
NOT(REGEX(Fax, "\\D*?(\\d\\D*?){10}")),
NOT(REGEX(HomePhone,  "\\D*?(\\d\\D*?){10}")),
NOT(REGEX(OtherPhone, "\\D*?(\\d\\D*?){10}"))
)

Found this on the Formula Examples page;

https://na1.salesforce.com/help/doc/en/fields_useful_field_validation_formulas.htm#ContactValidationRules 
Andrine ClarkeAndrine Clarke
This doesn't work because I also need to be able to leave some of the these field blank.
Andrine ClarkeAndrine Clarke
I figured it out. Thanks to Force Guru at http://forceguru.blogspot.com/2011/05/validation-over-apex.html. Here's the validation rule, I ended up using.

OR(
IF(
ISBLANK(Phone) , false , NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(MobilePhone) , false , NOT(REGEX(MobilePhone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(Fax) , false , NOT(REGEX(Fax, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(HomePhone) , false , NOT(REGEX(HomePhone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(OtherPhone) , false , NOT(REGEX(OtherPhone, "\\D*?(\\d\\D*?){10}"))),
IF(
ISBLANK(AssistantPhone) , false , NOT(REGEX(AssistantPhone, "\\D*?(\\d\\D*?){10}")))
)
This was selected as the best answer