开发者

Restricting user entering html tag in textbox using validation control in asp.net c#

开发者 https://www.devze.com 2023-03-02 15:29 出处:网络
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.

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='&lt;, &gt;, ", 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消