Regex for Email List on Salesforce Validation Rule

emailregular-expressionsvalidation-rule

**UPDATE 22 June 2022

For simplicity sake, I will edit my question to have emails separated with just semicolon e.g. [email protected];[email protected]


So I have this Long Text Area field to capture multiple email addresses inside like :

[email protected]; [email protected]

And I want to create a Salesforce Validation rule to ensure email list are well formed separated by ; or ,.

I tried using the Regex I found on this exchange and other sites to come up with :

NOT(REGEX(CS_Email_List__c  ,"(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"))

But I can never get it correct. Help pls?

Best Answer

Try with below validation rule separated by comma(,) or semi colon(;).

OR(
AND(NOT(
REGEX( CS_Email_List__c , "([a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z,]{2,4})*")
),
NOT(
REGEX( CS_Email_List__c , "([a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z;]{2,4})*")
)),
AND(RIGHT( CS_Email_List__c  , 1 ) = ",",

RIGHT( CS_Email_List__c , 1 ) = ";")


)
Related Topic