简体   繁体   中英

ASP.NET MVC Regular Expression for Username is not working correctly. or Convert Regex->JS

I have added a regular expression from a site to verify user name and and it should work but it is giving some error on the compile time. Please see the image and then I googled and learned that few of chars like '\\w' is not going to work because js does not support it. Now I don't know how to convert it , can anyone please help to convert this to workable with ASP.NET MVC data-annotations.

[RegularExpression("^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$")]

Thank you all in advance.

Make your string a literal by adding @ sign before the opening quote. Otherwise you would need to escape all the backslashes that the string contains. That would make regular expression like this even less readable.

[RegularExpression(@"^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$")]

A literal string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters. This makes literal strings ideal for file paths that naturally contain many backslashes. To create a literal string, add the at-sign @ before the string's opening quote

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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