0
votes

There is a need for client unobtrusive validation on the textbox to warn user that he types invalid symbols. I want to use RegularExpression in data annotation, somethimg like this:

[Required]
[Display(Name = "RecordBody", ResourceType = typeof(Resources))]
[RegularExpression(@"(\<(/?[^\>]+)\>)", ErrorMessageResourceType = typeof(ValidationErrors), ErrorMessageResourceName = "DisallowHtml")]
[StringLength(8191, ErrorMessageResourceType = typeof(ValidationErrors), ErrorMessageResourceName = "TooLongEntry")]
public string Description { get; set; }

A regular expression is used on server side to validate user input and is used on client side in Javascript regular expression:

<textarea cols="20" data-val="true" data-val-disallowhtml="Поле Текст записи содержит недопустимые символы" data-val-disallowhtml-pattern="(\<(/?[^\>]+)\>)" data-val-length="Введено слишком много символов. Допустимо: 8191" data-val-length-max="8191" data-val-required="Требуется поле Текст записи." id="Description" name="Description" rows="2"></textarea>

(\<(/?[^\>]+)\>) doesn't work because it matches only html tags. I need a regex that matches all content that is not an HTML tag. In other words to negate the whole regex.

Here is a working example http://cafuego.net/2011/11/15/html-and-regular-expressions. But i suppose (?<=^|>)([^><]+?)(?=<|$) it works only in php.

1
What exactly do you want to allow and prevent?Moby's Stunt Double
So what form exactly must be the form or the content of the textbox? Anything except HTML tags? That's sounds like something that should be just HTML-escaped.Loamhoof
I want prevent html tags. text text - valid. text text <b> text </b> - invalid and user is warned he typed invalid signsolly
Can't you just HTML-escape the input? I can't see any reason to forbid using html tags if they'll be escaped.Loamhoof

1 Answers

0
votes

Based on one of your comments, you should be able to use a lookahead assertion to search for any tags; if one is found, then the assertion will fail, and the error message should be displayed.

e.g.

[RegularExpression(@"^(?![^<]*<[^>]+>).*$", ...