Reputation: 1564
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(@"<[^>]*>",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.
Upvotes: 4
Views: 7757
Reputation: 11
RegularExpression to avoid any html tags entry use:
[RegularExpression("^[^<>,<|>]+$", ErrorMessage = "Html tags are not allowed.")]
Upvotes: -1
Reputation: 7277
You have to logic turned around. The regex you have written is what you do not want to allow, whereas the RegularExpression attribute requires you to enter what you do allow. Anything not matching your regex will show the ErrorMessage.
An alternative regex could be:
@"[^<>]*"
which would disallow < and >.
Upvotes: 8