I have page user creation and it contains textbox control. I want to restrict user entering html tag in i.e < and > sign in textbox using .net validation control.
Can an开发者_StackOverflow中文版y one help me about in this?
I also want to restrict double quote i.e " and caret sign ^ can you please tell me how to write expression for that???
Use a regularexpressionvalidator...
<asp:textbox id="theTextbox" runat="server" />
<asp:regularexpressionvalidator id="regexValiator" runat="server"
controltovalidate="theTextbox"
errormessage='<, >, ", and ^ not allowed'
display="Dynamic"
validationexpression='([^<>\"\^])*' />
Actually, by default ASP.Net dissallow HTML content to be entered in form fields. No need for further validation.
you can try the following code in your aspx code:
<asp:textbox id="txtBox" runat="server" />
<asp:RegularExpressionValidator controltovalidate="txtBox" ValidationExpression="([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_])" ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>
and now you can modify the regular expression to fit your case.
精彩评论