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))
)`
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.