function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dave The RaveDave The Rave 

Validation Formula to allow blank value & REGEX for email

The REGEX code below works perfectly, but I now need to allow for the email field also to be blank.

NOT(REGEX(Email ,('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+')))

Can I incorporate NOT ISBLANK( Email ) into the statement above so:

A user can enter an email in the correct format as above or the user does not have to enter an email address at all?

Thanks,

​​​​​​​Dave

 
Best Answer chosen by Dave The Rave
VinayVinay (Salesforce Developers) 
You can try using AND instead of OR.
AND(NOT(REGEX(Email ,('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+'))), NOT(ISBLANK(Email)))
Thanks,

All Answers

VinayVinay (Salesforce Developers) 
Hi Dave,

That should work,  do you see any error can you try and post if you see any errors?

Thanks,
Dave The RaveDave The Rave
Hi Vinay, I did not see your answer. I want to use the above REGEX statement but amend the validation rule. So the user is allowed to leave the email field blank and save a record or the user fills in an email address which conforms to the regex code.

(NOT(REGEX(Email,('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+')))) OR (NOT(ISBLANK) Email)

I do not think the format is correct, not enough brackets () ???. So Email is not an obligatory field
 
VinayVinay (Salesforce Developers) 
Try below code.
OR(NOT(REGEX(Email ,('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+'))), NOT(ISBLANK(Email)))
Thanks,
Dave The RaveDave The Rave
Hi Vinay, this does not work. If you do not fill in the email field your get the error message from the validation rule. There are no syntax errors

The statement NOT(ISBLANK(Email)) does work on its own though and also the REGEX, it is just a case of combining them together in the right formula.
VinayVinay (Salesforce Developers) 
You can try using AND instead of OR.
AND(NOT(REGEX(Email ,('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+'))), NOT(ISBLANK(Email)))
Thanks,
This was selected as the best answer
Dave The RaveDave The Rave
Hi Vinay, thankyou, the formula with <AND> works perfectly.