Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression validator is not allowing non-english characters for \w

I have email field in my page, which i am validating using regular expression validator provided my asp.net. I am using same validation expression as given along with validator for emails i.e

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

It is working fine but problem comes when I tried giving non-english letters e.g.

è é ü û ă etc.

But my problem is, when i use same expression in javascript it allows these characters, even at server side also same expression allows these characters.

I think '\w' allows all alphanumeric characters as well as non english characters but I dont know why it is not allowing when using it in validator.

Please suggest if I did anythig wrong.

like image 399
Rahul Avatar asked Sep 24 '10 10:09

Rahul


3 Answers

\w means word character. And the definition of a word character may differ from implementation to implementation. Some do only use [A-Za-z0-9_] while others also include non-US-ASCII characters (see “ascii-only” in comparison of regular expression flavors).

If you want to make sure that the same characters are used, list them explicitly like [A-Za-z0-9_èéüûă].

like image 63
Gumbo Avatar answered Nov 16 '22 17:11

Gumbo


This is a limitation of the ECMAScript standard; f.e. in .NET \w does also match non-english chars.

The simplest solution is to turn off client-side validation as you are working with ASP.NET, so the server-side validator (which uses the .NET implementation) will validate accordingly.


var r = new Regex(@"\w");
foreach(var m in r.Matches("è é ü û a"))
      Console.WriteLine(m);

Output:
è
é
ü
û
a
like image 1
Jan Jongboom Avatar answered Nov 16 '22 17:11

Jan Jongboom


It's a known issue: ASP.Net regular expression client-side validator is buggy for non-English characters. You may either use server-side validation (if it's an option), or write your own client-side CustomValidator.

like image 1
buru Avatar answered Nov 16 '22 18:11

buru