+ Start a Discussion
Raymond AireyRaymond Airey 

Validation Rule around Phone Number Entry

Hi All,

I am trying to write a validation rule to make sure a phone number is entered in the correct format every time.

A phone number should be entered in the following format:
For Australian Customers - 00 1111 2222
For New Zealand Customers - 00 111 2222

I have been trying to recycle the below code to use with this rule, trying to use the REGEX function but I dont think that works.
OR( 
AND( 
OR( 
ShippingCountry = "VIC", 
ShippingCountry = "NSW", 
ShippingCountry = "ACT", 
ShippingCountry = "QLD", 
ShippingCountry = "SA", 
ShippingCountry = "WA", 
ShippingCountry = "TAS", 
ShippingCountry = "NT", 
ShippingCountry = "Australia" 
), 
NOT(ISPICKVAL(CurrencyIsoCode, "AUD")) 
), 
AND( 
ShippingCountry = "NEW ZEALAND", 
NOT(ISPICKVAL(CurrencyIsoCode, "NZD")) 
) 
)

Raymond
Ankur Saini 9Ankur Saini 9
Hi Raymond Airey,
try this:

For this validation rule, i have created a field mobile validate of text type. the regex can be applied on text type field easily. 

If Your shipping country field is of text type try this:

(Type = 'Prospect' && NOT( REGEX( Moblie_validate__c, '[0-9]{2}[ ][0-9]{4}[ ][0-9]{4}'))&& Moblie_validate__c!=null)|| 
(Type = 'Other' && NOT( REGEX( Moblie_validate__c, '[0-9]{2}[ ][0-9]{3}[ ][0-9]{4}')) && Moblie_validate__c!=null)

If Your shipping country field is of picklist type try this:

(ISPICKVAL( Type , 'Prospect') && NOT( REGEX( Moblie_validate__c, '[0-9]{2}[ ][0-9]{4}[ ][0-9]{4}'))&& Moblie_validate__c!=null)|| 
(ISPICKVAL( Type , 'Other') && NOT( REGEX( Moblie_validate__c, '[0-9]{2}[ ][0-9]{3}[ ][0-9]{4}')) && Moblie_validate__c!=null)

Thanks
Ankur Saini
http://mirketa.com
Igor PetrovychIgor Petrovych
Phone validation is quite complex subject. You can check out the code and docs for libphonenumber to get some insight. I think using a libphonenumber (http://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000G12oJUAR) port to parse/validate the number in the apex trigger would give you a more precise result. Regex will not handle all possible cases.