简体   繁体   English

正则表达式验证器不允许\ w的非英语字符

[英]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. 我的页面中有电子邮件字段,我正在使用我的asp.net提供的正则表达式验证器进行验证。 I am using same validation expression as given along with validator for emails ie 我正在使用与电子邮件验证器一样的验证表达式,即

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

It is working fine but problem comes when I tried giving non-english letters eg 它工作正常,但当我尝试提供非英文字母时出现问题,例如

è é ü û ă 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. 但我的问题是,当我在javascript中使用相同的表达式时,它允许这些字符,即使在服务器端,同样的表达式也允许这些字符。

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. 我认为'\\ w'允许所有字母数字字符以及非英文字符,但我不知道为什么在验证器中使用它时不允许。

Please suggest if I did anythig wrong. 如果我做错了,请建议。

\\w means word character. \\w表示单词字符。 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 ). 有些只使用[A-Za-z0-9_]而有些也包含非US-ASCII字符(在正则表达式风味比较中参见“ascii-only”)。

If you want to make sure that the same characters are used, list them explicitly like [A-Za-z0-9_èéüûă] . 如果要确保使用相同的字符,请像[A-Za-z0-9_èéüûă]一样明确列出它们。

It's a known issue: ASP.Net regular expression client-side validator is buggy for non-English characters. 这是一个众所周知的问题:ASP.Net正则表达式客户端验证器是非英语字符的错误。 You may either use server-side validation (if it's an option), or write your own client-side CustomValidator. 您可以使用服务器端验证(如果它是一个选项),也可以编写自己的客户端CustomValidator。

This is a limitation of the ECMAScript standard; 这是ECMAScript标准的限制; fe in .NET \\w does also match non-english chars. 在.NET \\w中的fe也匹配非英语字符。

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. 最简单的解决方案是在使用ASP.NET时关闭客户端验证,因此服务器端验证器(使用.NET实现)将相应地进行验证。


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

Output:
è
é
ü
û
a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM