Z.ExtensionMethods icon indicating copy to clipboard operation
Z.ExtensionMethods copied to clipboard

IsValidEmail() Improved Regex Pattern

Open Huntk23 opened this issue 6 years ago • 0 comments

Made a slight update to the Regex Pattern to conform more to the rules of RFC 5322 which obsoletes RFC 2822.

I ended up updating the regex pattern because the built in .NET MailAddressParser conforms to RC2822 and did not want to re-implement to conform to RC5322. The supplied regex pattern should match ~99% of valid emails.

More tests have been implemented.

Old regex pattern:

--- Valid Email List ---
True : [email protected]
True : [email protected]
True : [email protected]
False : [email protected]
True : [email protected]
True : email@[123.123.123.123]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]

--- Invalid Email List ---
False : plainaddress
False : #@%^%#$@#$@#.com
False : @example.com
False : Joe Smith <[email protected]>
False : email.example.com
False : email@[email protected]
True : [email protected]
True : [email protected]
True : [email protected]
False : [email protected] (Joe Smith)
False : email@example
True : [email protected]
False : [email protected]
True : [email protected]

New regex pattern:

--- Valid Email List ---
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : email@[123.123.123.123]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]

--- Invalid Email List ---
False : plainaddress
False : #@%^%#$@#$@#.com
False : @example.com
False : Joe Smith <[email protected]>
False : email.example.com
False : email@[email protected]
False : [email protected]
False : [email protected]
False : [email protected]
False : [email protected] (Joe Smith)
False : email@example
False : [email protected]
False : [email protected]
False : [email protected]

For curiosities sake;

MailAddress constructor try/catch:

--- Valid Email List ---
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : email@[123.123.123.123]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]
True : [email protected]

--- Invalid Email List ---
False : plainaddress
False : #@%^%#$@#$@#.com
False : @example.com
True: Joe Smith <[email protected]>
False : email.example.com
False : email@[email protected]
False : [email protected]
True : [email protected]
True : [email protected]
True : [email protected] (Joe Smith)
True : email@example
True : [email protected]
True : [email protected]
True : [email protected]

Fixes #27

Huntk23 avatar Dec 17 '19 19:12 Huntk23