How do I restrict charaters to typed in the textbox?

drewgeezmoe

Member
Joined
Feb 8, 2007
Messages
9
Programming Experience
Beginner
Hi, I need a sample code to restrict the characters inputed in my text box. The characters that the text box should allow are: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

This textbox is for a webform.


Thanks
 
Last edited:
You could use the regular expression validator control to validate the textbox.

Here is the sample HTML code..

VB.NET:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="Invalid Character Entered" ValidationExpression="[A-Za-z]*">*</asp:RegularExpressionValidator><br />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
 
I had an error while trying to use the validator control. This is the error that shows:

Unable to find control id 'TextBox' referenced by the 'ControlToValidate' property of 'RegularExpressionValidator1'.

[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Unable to find control id 'TextBox' referenced by the 'ControlToValidate' property of 'RegularExpressionValidator1'.[/FONT]
Here is the code:
VB.NET:
                        <td style="width: 100px">
                            <asp:TextBox ID="UserName" runat="server" BorderStyle="None" Width="378px" BackColor="#F7F5EA" TabIndex="1" MaxLength="20"></asp:TextBox></td>
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox"
                                         ErrorMessage="Invalid Character Entered" ValidationExpression="[A-Za-z]*">*</asp:RegularExpressionValidator>
                                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
 
Back
Top