0

I'm using this regular expression

^(?=.{0,150}$)\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

for validating Email Addresses. when I Enter this Email Address : konstinkö[email protected] . It is working using Regular expression validator(it is showing InValidEmail Address), but when validating in C# code it is not working(taking it as validEmail Address)

return System.Text.RegularExpressions.Regex.IsMatch(
    strEmailAddress,
    @"^(?=.{0,150}$)\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
    System.Text.RegularExpressions.RegexOptions.IgnoreCase
);

4 Answers 4

1

If I understand your problem you want the address: konstinkö[email protected] to fail validation because it contains Unicode characters. Your regex uses the \w character class which by default matches any word characters, this includes any Unicode characters defined as letters.

You can force \w to be just [a-zA-Z_0-9] by specifying RegexOptions.ECMAScript you code becomes:

return System.Text.RegularExpressions.Regex.IsMatch(
    strEmailAddress,
    @"^(?=.{0,150}$)\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
    System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.RegexOptions.ECMAScript
);

Alternatively you could replace the \w with [a-zA-Z_0-9] which would have the same effect.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , Its Working !! I have used two regular expressions to solve this issue before.
0

End you Regex with $ as an example see this. http://www.dotnetperls.com/regex-match

Comments

0

Try to use this one:

Regex.IsMatch(strEmailAddress, @"^[\\p{L}0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[\\p{L}0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[\\p{L}0-9](?:[\\p{L}0-9-]*[\\p{L}0-9])?\.)+[\\p{L}0-9](?:[\\p{L}0-9-]*[\\p{L}0-9])?$", RegexOptions.IgnoreCase);

it will admit non ASCII character

3 Comments

It should not admit Non-Ascii Character, I need a regular expression for not accepting Non-Ascii characters ,Special Characters and SPaces
You have to add this condition: [^\x00-\x7F].
@user3270152 ö is not ASCII. Maybe I misunderstood the problem. In any case, in .NET/C#, \w includes non-ASCII Unicode
0

I think a better solution would be using the System.Net.Mail.MailAddress class to validate the email address.

Try using the following code:

try {
    MailAddress addr = new System.Net.Mail.MailAddress(email);
    return true;
}
catch {
    return false;
}

UPDATE Regex solution:

bool d = Regex.IsMatch("konstinkö[email protected]", @"^([\p{L}\.\-\d]+)@([\p{L}\-\.\d]+)((\.(\p{L}){2,63})+)$", RegexOptions.IgnoreCase);

Works for me.

3 Comments

I tried this one aswell , it is not working... it should not admit Non-Ascii Character
Using a Try/Catch isn't the best way to make a test. You need to test is something accord to a condition, not try to do something and look if it failed or not.
@Eliran , I tried this , it is not working ... it is returning true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.