Ram Singh
Ram Singh

Reputation: 6918

How to ignore Email Address with Ipaddress in c#

i am using the following Regular Expression to validate the Email Address, i am using server side Validations.

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

The above expression validating the email address perfectly, but it is not validating [email protected] type of email addresses.

i don't the email addresses with IP-Address as domain name to get validate test pass. i have googled but didn't found any helpful result.

Please help me. My question is : [email protected] should not pass the validate test.

Upvotes: 0

Views: 120

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can use a look ahead following @ so as to ensure that there is atleast 1 alphabet following @

/\w+([-+.']\w+)*@(?=.*[a-zA-Z])\w+([-.]\w+)*\.\w+([-.]\w+)*/
  • (?=.*[a-zA-Z]) Positve look ahead. Checks if the @ contiains an alphabet. If this match returns true, then proceeds with the remaining pattern. Else would discard the entire string.

Regex Demo

Test

Regex pattern = new Regex(@"^\w+([-+.']\w+)*@(?=.*[a-zA-Z])\w+([-.]\w+)*\.\w+([-.]\w+)*$");

Console.WriteLine( pattern.IsMatch("[email protected]"));
=> True

Console.WriteLine( pattern.IsMatch("[email protected]"));
=> False

Upvotes: 3

Related Questions