0

I wrote a validation rule that specifies the amount of characters in the field and the way those characters are suppose to be placed. However to go with different wrtiting 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 addiditonal 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.

3
  • This question isn't really specific to Salesforce and you will probably get faster regex help on Stack Overflow.
    – Adrian Larson
    Commented Nov 22, 2017 at 13:33
  • Are these new prefixes supposed to optional for Portugal? Are they supposed to relplace the first four digits?
    – Adrian Larson
    Commented Nov 22, 2017 at 13:35
  • these prefixes are suppose to be optional for both of the above examples. They aren't suppose to replace the first 4 digits rather add an optional "P-" or "PT-" to the already existing rule.
    – user26057
    Commented Nov 22, 2017 at 13:46

1 Answer 1

0

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 consistent.

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 -) to the beginning of your clause (after the start of string character, of course. For example:

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

You must log in to answer this question.