如何防止用户在TextBox中放入特殊字符 [英] How can I Prevent users to put special characters in a TextBox

查看:108
本文介绍了如何防止用户在TextBox中放入特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止用户在TextBox中输入,等特殊字符。

Textbox是一个只能接受名字的搜索框(仅限a到z和0-9)。



我想使用自定义验证器,但不知道在C#代码中放什么。



如何我可以吗?





*********

< asp:CustomValidator ID =   CustomValidator1 runat =   server 
ControlToValidate = SearchTextBox
ErrorMessage = 不允许使用特殊字符
onservervalidate = CustomValidator1_ServerValidate />



************

<前lang =cs> 受保护 void ServerValidate( object sender,ServerValidateEventArgs e)
{
// 这里需要代码。
}





谢谢..

解决方案

对于文本框中的任何字母数字要求,我使用内置验证器,使用ajaxtoolkit扩展窗口。





< asp:TextBox ID =txtIDrunat =serverWidth =50%MaxLength = 10 >< / ASP:文本框> 
< asp:Label ID =lblRequiredFieldIndicator0runat =serverFont-Bold =TrueForeColor =RedText =*>< / asp:Label>
< asp:RegularExpressionValidator ID =regExIDForeColor =Redrunat =serverHeight =24pxWidth =192pxControlToValidate =txtIDDisplay =NoneValidationExpression =^ [0 -9a-zA-Z - ] +


ErrorMessage =仅字母数字字符>< / asp:RegularExpressionValidator>
< ajaxToolkit:ValidatorCalloutExtender runat =ServerID =vceRegExIDTargetControlID =regExIDHighlightCssClass =validatorCalloutHighlightWidth =250px/>
< asp:RequiredFieldValidator runat =serverID =reqIDControlToValidate =txtIDDisplay =NoneErrorMessage =< b>必填字段缺失< / b>< br />否特殊字符允许。 />
< ajaxToolkit:ValidatorCalloutExtender runat =ServerID =vceIDTargetControlID =reqIDHighlightCssClass =validatorCalloutHighlightWidth =250px/>


我不明白你的C#问题。对于任何字符 ch 你,可以轻松检查它是否是某些坏字符的元素,或者可以在一组好字符中找到它:



  if (System.Char.IsLetter(ch)| | System.Char.IsDigit(ch)) //   ... good one  

//

string badCharacters = 。; *?^&; // 无论
如果 (badCharacters.Contains(ch)) // ... bad one

// 或者,您可以通过拉丁语和数字来限制好字符......





但是,如果你想在文本框中限制字符输入,你最好不要在C#中这样做,因为每一个输入你将有一个回发,这将减慢超出任何可容忍的东西。然后你需要根据字符代码点过滤掉按键的JavaScript。这也很容易。



-SA


I want to prevent users from entering Special characters like " ,' etc in a TextBox.
Textbox is a search box which can accept names only (a to z and 0-9 only).

I want to use a Custom validator but no idea what to put in C# code.

How can I do it?


*********

<asp:CustomValidator ID="CustomValidator1" runat="server"
                    ControlToValidate="SearchTextBox"
                    ErrorMessage="Special Characters are not allowed"
                    onservervalidate="CustomValidator1_ServerValidate" />


************

protected void ServerValidate(object sender, ServerValidateEventArgs e)
{
   //need code here.
}



Thanks..

解决方案

For any alphanumeric requirements in a textbox I use the built in validator with an extention for popup window using the ajaxtoolkit.


<asp:TextBox ID="txtID" runat="server" Width="50%" MaxLength="10"></asp:TextBox>
<asp:Label ID="lblRequiredFieldIndicator0" runat="server" Font-Bold="True" ForeColor="Red" Text="*"></asp:Label>
<asp:RegularExpressionValidator ID="regExID" ForeColor="Red" runat="server" Height="24px" Width="192px" ControlToValidate="txtID" Display="None" ValidationExpression="^[0-9a-zA-Z-]+


" ErrorMessage="Only Alphanumeric characters"></asp:RegularExpressionValidator> <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="vceRegExID" TargetControlID="regExID" HighlightCssClass="validatorCalloutHighlight" Width="250px" /> <asp:RequiredFieldValidator runat="server" ID="reqID" ControlToValidate="txtID" Display="None" ErrorMessage="<b>Required Field Missing</b><br />No Special Characters allowed." /> <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="vceID" TargetControlID="reqID" HighlightCssClass="validatorCalloutHighlight" Width="250px" />


I don't understand your problem with C#. For any character ch you, can easily check if it is an element of some set of "bad" characters, or it can be found in a set of the "good" characters:

if (System.Char.IsLetter(ch) || System.Char.IsDigit(ch)) //... good one

// or

string badCharacters = ".;*?^&"; // whatever
if (badCharacters.Contains(ch)) //... bad one

// or, you can limit "good" characters by just Latin and digits...



However, if you want to constraint the character input in, say, text box, you should better not do it in C#, because on every input you will have a postback which would slow down things beyond anything tolerable. Then you need to have a JavaScript filtering out key presses based on the character code point. This is easy enough, too.

—SA


这篇关于如何防止用户在TextBox中放入特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆