How does Salesforce.com validate Email Fields?
Asked Answered
K

7

7

I'm trying to store email addresses in Salesforce.com from another service that allows invalid email addresses to be specified. If one of those bad invalid email addresses is sent to Salesforce.com via their Web Services API, Salesforce.com will prevent the record from saving with an INVALID_EMAIL_ADDRESS error code.

I can't find any documentation on how to disable validation on Email fields, so it looks like I'll need to validate them in my integration and pull out those that fail. Does anyone know the validation process Salesforce.com uses to determine if an email address is valid? All I have right now is a Regex, but I'd like it to match Salesforce.com's process.

EDIT: For reference, here is my Regex (I'm using C#/.NET):

^(\w|[!#$%'*+-/=?^_`\{\}~.&])+@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$
Karp answered 24/3, 2012 at 13:27 Comment(3)
Your regex suffers from some issues, as most expressions targeted at email validation; some examples: [email protected], [email protected], this."is a valid"[email protected] - Please see my answer for a .NET-based alternative. :)African
Rather than trying to validate them the same way, why can't you just wait til get the error from the server,then handle it.Warmhearted
That's another way to handle it, @superfell, though that would less desirable from a performance perspective.Karp
L
9

Summary: we're using the following .NET RegEx:

const string SFEmailRegExPattern = @"^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";

If you can believe SF's own documentation then:

For the local part of the email address we accept the following characters. The local part is anything before the @ sign.

abcdefg.hijklmnopqrstuvwxyz!#$%&'*/=?^_+-`{|}~0123456789

Note: The character dot . is supported; provided that it is not the first or last character in the local-part

For the domain part of the email address we accept. The domain part is anything after the @ in an email address:

0-9 and A-Z and a-z and dash -

A couple of people have coded this up as a Java regex as:

String pat = '[a-zA-Z0-9\\.\\!\\#\\$\\%\\&\\*\\/\\=\\?\\^\\_\\+\\-\\`\\{\\|\\}\\~\'._%+-]+@[a-zA-Z0-9\\-.-]+\\.[a-zA-Z]+';

although to me this looks like it fails to reject an email that starts with a "." so isn't perfect.

Lavern answered 19/9, 2012 at 10:28 Comment(2)
"we're using..." I gotta ask. Do you work for Salesforce.com? Did you just pull that out of the source?Karp
Nope, possibly poorly worded, sorry. I couldn't work for SF and hold my head up in the community. They must really hate software engineers: #1665003Lavern
A
1

I don't know how salesforce.com is validating email addresses, but since you are using .NET I'd suggest you to consider an email validation component like our EmailVerify.NET, which is 100% compliant with the current IETF standards (RFC 1123, RFC 2821, RFC 2822, RFC 3490, RFC 3696, RFC 4291, RFC 5321, RFC 5322 and RFC 5336) and does not suffer from ReDoS: if needed, it even checks the DNS records of the email domain under test, its SMTP availability, validates the related mailbox and can even tell if the target mail exchanger is a catch-all or if it is a disposable/free email address provider.

African answered 25/3, 2012 at 6:48 Comment(0)
G
0

I don't know what salesforce.com uses (and I don't think there's any way for you to find out), but \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b from here is a commmon one and should work for most of the cases.

Gale answered 24/3, 2012 at 13:36 Comment(3)
Yeah, as I stated in the question, I have a Regex. I'm sure it will catch 99.9% of cases. I'm trying to find out anything I can about potential outliers.Karp
maybe include your regex in the answer so people can look it overGale
Unfortunately, this regex will work for some cases, not most. Some examples: [email protected], [email protected], [email protected], [email protected] and way more. Just ask and I will keep enumerating. :)African
E
0

I've looked previously and not been able to find a definitive answer on exactly which rules SFDC applies to the native "Email" field type. The quickest path to success that I would suggest would be this:

  • in your initial data integration from the external application, map the email field that you describe into a new (non-email, just text 255) custom field in SFDC.

  • if this is a one-time dataload, run a separate process that, for every row in SFDC with this custom field populated, attempts to copy the contents of this custom field to the native email field. If any row fails with the email validation error, you just skip it. Then you can decide what to do with the non-compliant addresses.

  • if this is an ongoing integration, it may be better to do something like attempt to insert new rows one-at-a-time via WS API, and if the email validation exception is thrown, you catch it and either insert the record without an email address, store the bad email in a different field (like a custom field called "non-compliant email address"), or skip the row altogether (if bad emails == bad record).

Hope that helps.

Exhalant answered 26/3, 2012 at 1:27 Comment(0)
S
0

Apex has native Pattern and Matcher classes, based on java.

You can validate your email addresses in Apex code, using your RegEx expression as a string

String emailPattern = {your regex expression);
Boolean validEmail = pattern.match(emailPattern, emailAddress);
Squirmy answered 23/5, 2012 at 18:45 Comment(0)
S
0

You can't definitely create common regex for salesforce email, due to inconsistency of their own requirements.

The one rule is about to give possibilities to put IP address after the local part. Example -> [email protected].

The second is about do not allow digits in top-level domain. For example: [email protected]

So, they are mutually excluded.

But as I understood the email address with IP after the local part is more important and commonly used comparing with numbers in top-level domain.

Here is some examples of valid/invalid emails for salesforce.

Valid:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Alexka1!+1123klsn&*^%$%$#^^^@a3432.4s.c4p.uk
frw...??//||/wt'f`[email protected]
[email protected]
abcdefg.hijklmnopqrstuvwxyz!#$%&'*/=?^_+-`{|}[email protected]

Invalid:

aasd@sdfжжж.rf
plainaddress
#@%^%#$@#$@#.com
@domain.com
email.domain.com
email@[email protected]
[email protected]
あいうえお@domain.com
[email protected] (Joe Smith)
email@domain
[email protected]
[email protected]
[email protected]

As result of above, the final regex is:
/^(?!\.)(([^<>()\[\]\\a-zA-Z0-9.,;:\s@"]*(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))[a-zA-Z0-9.!#$%&'‘*+\/=?^_{|}~-]+@[\w.-?]+.[A-Za-z]*(?

Sturges answered 5/7, 2019 at 10:41 Comment(1)
In the version of Salesforce I'm using, [email protected] and [email protected] are both valid email addresses.Terzetto
T
0

Here is a regular expression based on this help page + a lot of experimenting in Salesforce:

^(?=(?:\([^)]*\))*[^()]+[^@]*@)(?!(?:\([^)]*\))*\.)(?:(?:[\w!#$%&'*+/=?^`{|}~.-]|\([\w!#$%&'*+/=?^`{|}~.-]*\))+|"(?:[\w!#$%&'*+/=?^`{|}~.-]|\([\w!#$%&'*+/=?^`{|}~.-]*\))*")@(?:\([A-Za-z0-9-]*\))*(?:[A-Za-z0-9]+(?:(?:\([A-Za-z0-9-]*\))*-(?:\([A-Za-z0-9-]*\))*[A-Za-z0-9]+)*(?:\([A-Za-z0-9-]*\))*)(?:\.(?:\([A-Za-z0-9-]*\))*(?:[A-Za-z0-9]+(?:(?:\([A-Za-z0-9-]*\))*-(?:\([A-Za-z0-9-]*\))*[A-Za-z0-9]+)*(?:\([A-Za-z0-9-]*\))*))+$

See this Demo. It gives the same validation result as Salesforce for all the values I could think of testing - copied below - any counter examples are welcome...

************* VALID *************
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
!#$%&'*+-/=?^_`{|}[email protected]
[email protected]
[email protected]
"a"@test.jp
""@test.jp
(comment)(comment)a(comment)(comment)(comment)@(comment)a.a
(comment)(comment)a.(comment)(comment)(comment)@(comment)a.a
(comment)(comment)a(comment).(comment)(comment)@(comment)a.a
a@(comment)a(comment)-(comment)a(comment).a
john.doe@(-comment)example.com
[email protected](comment-)
()[email protected]
(a)[email protected]
a(a)@test.jp
a@(a)test.jp
[email protected](a)
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
test/[email protected]
[email protected]
"john..doe"@example.org
[email protected]
user%[email protected]
[email protected]
1@1234567890123456789012345678901234567890123456789012345678901234.1.2.3.4.5.6.7
[email protected]
[email protected]
[email protected]
[email protected]

************* INVALID *************
a@a
a@a.
[email protected]
[email protected]
[email protected]
[email protected]
a;[email protected]
[email protected]
";"@test.jp
"@"@test.jp
"[email protected]
a"@test.jp
a""@test.jp
""[email protected]
()@test.jp
)([email protected]
(a)@test.jp
([email protected]
(())[email protected]
(comment)(comment).(comment)a(comment)(comment)@(comment)a.a
john.doe@(comment).com
a@(comment)a(comment)-(comment)(comment).a
Αθήνα@email.com
admin@mailserver1
" "@example.org
"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
postmaster@[123.123.123.123]
postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]
Terzetto answered 14/2, 2023 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.