function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cased1919cased1919 

Validating multiple number formats in one validation rule

I'm attempting to validate a postal code for a country that currently has two different postal code formats (9999 and/or 9999-999)

 

Currently I'm trying the following:

 

IF (( 
Country__r.Name = 'Portugal') && 
(LEN( Zip_Postal_Code__c) >1), 
(NOT(REGEX(Zip_Postal_Code__c , "[0-9]{4}")) || NOT(REGEX(Zip_Postal_Code__c , "[0-9]{4}-[0-9]{3}"))),

NULL)

 

What would be the proper syntax for including two formats in one validation rule?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SrikanthKuruvaSrikanthKuruva

try the following. when ever the country is portugal zip code should be one of the formats ("[0-9]{4}" or "[0-9]{4}-[0-9]{3}"
).  you can add any additional formats easily.

AND(

Country__r.Name = 'Portugal',

NOT( REGEX( Zip_Postal_Code__c , "[0-9]{4}") ),

NOT( REGEX( Zip_Postal_Code__c , "[0-9]{4}-[0-9]{3}") )

)

All Answers

SrikanthKuruvaSrikanthKuruva

try the following. when ever the country is portugal zip code should be one of the formats ("[0-9]{4}" or "[0-9]{4}-[0-9]{3}"
).  you can add any additional formats easily.

AND(

Country__r.Name = 'Portugal',

NOT( REGEX( Zip_Postal_Code__c , "[0-9]{4}") ),

NOT( REGEX( Zip_Postal_Code__c , "[0-9]{4}-[0-9]{3}") )

)

This was selected as the best answer
cased1919cased1919

Thank you SrikanthKuruva for the help!!

 

For future views, below is my modified version since I only want to validate when a user fills in a value into this field.

 

IF ((
Country__r.Name = 'Portugal') &&
(LEN( Zip_Postal_Code__c) >1),
AND(
NOT( REGEX( Zip_Postal_Code__c , "[0-9]{4}") ),
NOT( REGEX( Zip_Postal_Code__c , "[0-9]{4}-[0-9]{3}") )
),
NULL)