0

I am working to validate the format of a string to 4 letters-3 zeroes-4 letters. On it's own, this REGEX() works fine: OR( /* check the leftmost 4 for any digit */ REGEX(LEFT(String__c,1),"[0-9]{1}"), REGEX(RIGHT(LEFT(String__c,2),1),"[0-9]{1}"), REGEX(RIGHT(LEFT(String__c,3),1),"[0-9]{1}"), REGEX(RIGHT(LEFT(String__c,3),1),"[0-9]{1}"), /* check the rightmost 4 for any digit */ REGEX(RIGHT(String__c,1),"[0-9]{1}"), REGEX(LEFT(RIGHT(String__c,2),1),"[0-9]{1}"), REGEX(LEFT(RIGHT(String__c,3),1),"[0-9]{1}"), REGEX(LEFT(RIGHT(String__c,4),1),"[0-9]{1}")) As it happens, there is an older format that is also valid, and when I include this ^ in the other validation it's ignored; unless I have this ^^ in it's own, separate validation rule; then they both fire.

`OR( /* Not just 5 numbers */ AND(LEN(String__c)==5, NOT(ISNUMBER(String__c)) ),

/* NOT 7 characters being an upper/lower combination of 'LX' and 5 numbers */ AND(LEN(String__c)==7, OR(NOT(UPPER(LEFT(String__c,2))=="LX"), NOT(ISNUMBER(RIGHT(String__c,5))) ) ),

/* not value with a digit in first- or last 4 characters / OR( / check the leftmost 4 for any digit / REGEX(LEFT(String__c,1),"[0-9]{1}"), REGEX(RIGHT(LEFT(String__c,2),1),"[0-9]{1}"), REGEX(RIGHT(LEFT(String__c,3),1),"[0-9]{1}"), REGEX(RIGHT(LEFT(String__c,3),1),"[0-9]{1}"), / check the rightmost 4 for any digit */ REGEX(RIGHT(String__c,1),"[0-9]{1}"), REGEX(LEFT(RIGHT(String__c,2),1),"[0-9]{1}"), REGEX(LEFT(RIGHT(String__c,3),1),"[0-9]{1}"), REGEX(LEFT(RIGHT(String__c,4),1),"[0-9]{1}")),

/* not 13 characters and in the format azAZ-###-azAZ */ AND( LEN(String__c)==13, NOT(REGEX(String__c,"[a-zA-Z]{4}-[0]{3}-[a-zA-Z]{4}")) ),

/* or anything not matching 5-, 7- or 13 characters */ AND(NOT(ISBLANK(String__c)), NOT(LEN(String__c)==5), NOT(LEN(String__c)==7), NOT(LEN(String__c)==13))

)`

3
  • NOTE: Some of the commented lines /* */ appear incorrectly formatted; they are fine and validated in the actual formula. Commented Mar 28, 2023 at 19:28
  • Is there a reason you don't just use a full regex for the whole value? E.g. REGEX('\w{4}\d{3}\w{4}'). What you have done is very complex for what looks like no reason. You can allow for different formats easily using the pipe (|) character. Have a look at regex101.com or other resources about regex.
    – Phil W
    Commented Mar 28, 2023 at 19:47
  • Thank you Phil; there are many resources for REGEX, with or w/o Salesforce, and as there are always many ways to approach a problem, it can get complicated. I will give yours a try and report back. Commented Apr 4, 2023 at 18:03

0

You must log in to answer this question.

Browse other questions tagged .