0

需要对文本框进行客户端不显眼的验证,以警告用户他键入了无效符号。我想在数据注释中使用正则表达式,如下所示:

[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; }

服务器端使用正则表达式来验证用户输入,并在 Javascript 正则表达式中用于客户端:

<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>

(\<(/?[^\>]+)\>)不起作用,因为它只匹配 html 标签。我需要一个匹配所有不是 HTML 标记的内容的正则表达式。换句话说,否定整个正则表达式。

这是一个工作示例http://cafuego.net/2011/11/15/html-and-regular-expressions。但我想(?<=^|>)([^><]+?)(?=<|$)它只适用于 php。

4

1 回答 1

0

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(@"^(?![^<]*<[^>]+>).*$", ...
于 2013-04-10T15:55:39.073 回答