Reputation: 781
How can I validate the following incorrect email addresses with regex in c#?
eg:
[email protected]
or
[email protected]
These are being validated as correct email addresses. Any thoughts? Thanks
Upvotes: 0
Views: 1637
Reputation: 6454
While both [email protected]
and [email protected]
are valid, from a syntactic point of view, their domain parts do not point to existing domains.
For this kind of test, you may want to extract the domain part you are interest in and query the DNS (see RFC 2821 and RFC 2822) to see if it exists.
Since you are using .NET, by the way, I would suggest you to take a look at our EmailVerify.NET, a leading email validation library which can validate the syntax (according to the latest IETF standards), the domain parts and the presence of a mailbox for your email addresses.
Upvotes: 2
Reputation: 3625
Please consider this regex:
([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})
It will match all the email-address, if it did not match the email address, the email-ad is not in correct format, Hope it helps :)
Upvotes: 0
Reputation: 573
You may want to just use something like:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
For a list, please see this page.
Upvotes: 1