在TextBox prevent特殊字符 [英] Prevent special characters in a TextBox
问题描述
我要prevent用户进入网址(如A HREF =)中的一个文本框。
I want to prevent users from entering url's (like a href="") in a TextBox.
我想用一个常规的前pression验证,但是不知道该写什么?
I want to use a regular expression validator but no idea what to write?
我该怎么办呢?
推荐答案
你的意思是你从字面上要prevent他们进入文本的href =
中文本框,或者你想prevent网址吗?<击>无论哪种方式,<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularex$p$pssionvalidator.aspx\"相对=nofollow> RegexValidator 是一个解决办法:击>
Do you mean you literally want to prevent them from entering the text href="
in the TextBox, or you want to prevent URLs? Either way, a RegexValidator is one solution:
其实,据我所知,没有使用一个开箱即用的正则表达式验证器做了负面包含一个非常简单的方法(即失败,如果任何匹配)。聪明的人也许能够纠正我这句话。但是你绝对可以使用自定义验证:
Actually, as far as I know there is not a very easy way to use an OOTB-regex validator to do a negative contains (i.e. "fail if any match"). Someone smarter may be able to correct me on that. But you can definitely use a custom validator:
<asp:TextBox runat="server" id="myTextBox" />
<asp:CustomValidator runat="server" OnServerValidate="ValidateNoUrls" ControlToValidate="myTextBox" ErrorMessage="URLs not allowed" />
codebehind:
Codebehind:
protected void ValidateNoUrls(object sender, ServerValidateEventArgs e)
{
e.IsValid = !Regex.IsMatch(e.Value, @"(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?");
}
这篇关于在TextBox prevent特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!