4

I want to prevent any html tags (written between "<>") in a textbox in my mvc 4 application. I have given the data annotation regular expression for my property as follows:

    [RegularExpression(@"&lt;[^>]*>",ErrorMessage="Invalid entry")]
    public string Name { get; set; }

But the regular expression not working correctly. When I type , it shows "Invalid entry".After that, when I type some normal text like "praveen" also shows "Invalid entry" error message.

I have tried another regular expressions something like @"<[^>]*>" ,but same result as above.

Please help.

4

2 回答 2

8

你必须把逻辑转过来。您编写的正则表达式是您不想允许的,而 RegularExpression 属性要求您输入您允许的内容。任何与您的正则表达式不匹配的内容都会显示错误消息。

另一种正则表达式可能是:

@"[^<>]*"

这将不允许 < 和 >。

于 2013-05-25T16:21:21.240 回答
-1

正则表达式以避免任何 html 标记条目使用:

[RegularExpression("^[^<>,<|>]+$", ErrorMessage = "Html tags are not allowed.")]
于 2013-12-20T09:31:09.120 回答