Can Adding an extra condition to a regex validation rule work efficiently?

646    Asked by ankur_3579 in Salesforce , Asked on Apr 22, 2021

I wrote a validation rule that specifies the amount of characters in the field and the way those characters are supposed to be placed. However to go with different writing styles I want to be able to add an extra regex condition to my rule. Here is a part of the existing rule:

AND( NOT(REGEX(BillingPostalCode, "\d{6}" )), 1 = CASE(BillingCountryCode, 'CN' , 1 , 'IN' , 1 , 'KZ' , 1 , 'RO' , 1 , 'RU' , 1 , 'SG' , 1 , 0 )), AND( NOT(REGEX(BillingPostalCode, "^(\d{4}(\-\d{3})?)?$")), 1 = CASE( BillingCountryCode, 'PT', 1 , 0 )),

so based on the code above China, Kazakhstan, Romania... etc have to have 6 characters in their post code. Portugal has to have either 4 or 4-3(8) Is there a way to add additional rules to the already existing rule so that I can have:

D-

PL-

ES-

FIN-

D -

All Optional in the beginning of the postal code. I haven't managed to do it in any way.

Answered by Arun Sharma

If you are facing a regex or condition in your coding then please note that beginning an expression with ^ and ending it with $ (^...$) make it so the expression will only match the entire string. You use this pattern in one expression but not the other. Whichever you desire, you should probably be compatible. As for adding optional clauses, you can use a ? after a grouping to make it optional. You can use parenthesis wrapped, pipe delimited options as an or clause (e.g. (A|B|C)). Putting that together you could add something like (D-|PL-|ES-|FIN-|D -) initially of your clause (after the start of string character, of course. For example:

  ^(D-|PL-|ES-|FIN-|D -)?\d{6}$


Your Answer

Interviews

Parent Categories