Click here to Skip to main content
15,886,045 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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?


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

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


Thanks..
Posted
Updated 8-Oct-13 7:07am
v5
Comments
Sergey Alexandrovich Kryukov 7-Oct-13 18:04pm    
There is no such thing as "special character", is there? What's so "special" in ','?
—SA
Coldcreek 7-Oct-13 18:06pm    
Like Quotation mark (") in search box.
Say you put the name "Peter" .
I want users to put only Peter (no quotations mark)

Thanks
Sergey Alexandrovich Kryukov 7-Oct-13 18:13pm    
Whatever it is, you need to strictly and comprehensively define your character sets, without any examples. Anyway, I answered, please see.
—SA

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" />
 
Share this answer
 
Comments
Coldcreek 7-Oct-13 19:11pm    
Thanks Jeewiz ..My question is cant we use < asp:CustomValidator> instead of Regularexpression.
Jeewiz2 8-Oct-13 9:00am    
You can use the customValidator, by using the ValidationExpression in the regExpValidator. The reason I avoid using the CustomValidator is due to it being a server side control so like Sergey said it will cause a postback in order to validate. If you want to avoid the post back you have to use jScript or VB in the browser to cause the client side validation which is where you want it. The RegExp with ajaxtoolkit takes care of the validation on the client side avoiding a postback.
Coldcreek 8-Oct-13 15:28pm    
Thanks for such nice Explanation.
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:

C#
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
 
Share this answer
 
v2
Comments
Coldcreek 7-Oct-13 19:01pm    
thanks Sergey.
Sergey Alexandrovich Kryukov 7-Oct-13 19:33pm    
You are very welcome. If you need a character filtering help with JavaScript, I'll gladly answer...
Good luck, call again.
—SA
Coldcreek 7-Oct-13 20:05pm    
Sure I will ....I know you will always be there for me ...
good night.
Sergey Alexandrovich Kryukov 7-Oct-13 20:15pm    
Good night.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900