0

ASP NET MVC5 web application, NET 4.6.1

Razor view:

@Html.EditorFor(model => model.message, new { htmlAttributes = new { @class = "form-control textarea", rows = "15", placeholder = Resources.field_required } })
@Html.ValidationMessageFor(model => model.message, "", new { @class = "text-danger" })

ViewModel:

[DataType(DataType.MultilineText)]
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "messagevalidation")]
[RegularExpression(@"^[?!,;.\p{L}0-9'-'\s]*$", ErrorMessage = "Please enter letters and numbers only")] 

By using implemented regex

^[?!,;.\p{L}0-9'-'\s]*$

or

^[?!,;.'-'\p{L}\d\s]*$

I am able to validate every Unicode alphabet and digit plus a basic set of punctuation marks, which is what I need, in many different regex testing sites (http://regexstorm.net for instance). Nevertheless, running the application, form fails on any input character while I had no problems with previous regex

^[?!,;.A-zÀ-ÿ0-9'-'\s]*$

but it did not validate Unicode (Cyrillic, Chinese, etc...)

Luke
  • 399
  • 4
  • 17
  • 1
    The regex is run on the client side, i.e. in JavaScript, that does not support Unicode category classes like `\p{L}`. Either list all the letter ranges (the pattern will be huge then) or implement [XRegExp](http://xregexp.com/) (that "knows" `\pL` construct), or only run validation on the server side with .NET regex. – Wiktor Stribiżew Jul 06 '17 at 20:53
  • Thanks Wiktor that matches with any information I could retrieve in last hours. It sounds a bit weird Jquery decided to ignore some billion Indians, Chinese, Arabs, Russians.... Unfortunately I am not a js expert and did not find any basic documentation explaining how to implement Unicode validation in ASP NET MVC forms using XregExp. – Luke Jul 06 '17 at 22:09
  • Do you think you want to list all the Unicode letter ranges? I already provided such [an answer](https://stackoverflow.com/a/37471102/3832970). – Wiktor Stribiżew Jul 06 '17 at 22:12
  • I just want Unicode characters, and not only extended ASCII, to be able to pass form validation. I only need to filter some potentially dangerous characters (< > for instance). I will have a look at your post for sure in case it's pertinent. – Luke Jul 06 '17 at 22:17
  • Not sure now what you mean by Unicode *characters*. I shared a link to the solution with all Unicode *letters*. If your requirement is not to make JS code work with `\p{L}` construct, it would be best to explain that in the question. – Wiktor Stribiżew Jul 06 '17 at 22:19
  • That is exactly what I wrote in my original post: server side validation (working as expected) is (quoting) ^[?!,;.'-'\p{L}\d\s]*$. Sorry that wasn't clear enough. – Luke Jul 06 '17 at 22:24

0 Answers0