Jump to content

Is there a way to prevent the user from entering numbers to a textbox in ASP.NET?

i need to display a error message if the user tries to input numbers to a text box,is it possible to do this?

Link to comment
Share on other sites

Link to post
Share on other sites

an if statement would help

a quick search:http://stackoverflow.com/questions/8321871/how-to-make-a-textbox-accept-only-alphabetic-characters

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]"))
    {
        MessageBox.Show("This textbox accepts only alphabetical characters");
        textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}
Link to comment
Share on other sites

Link to post
Share on other sites

On 1/11/2017 at 7:26 AM, Shammikit said:

i need to display a error message if the user tries to input numbers to a text box,is it possible to do this?

You can use a regular expression with an asp.net validator.

edit: here's an example, just throw it in a table...

 

<asp:TextBox ID="txtOnlyAlphabetical" runat="server" />
<asp:RegularExpressionValidator ID="revOnlyAlphabetical" runat="server" ValidationGroup="Submit" 
    ValidationExpression="^([^0-9]+)$" ControlToValidate="txtOnlyAlphabetical" 
    ErrorMessage="*" Font-Bold="true" ForeColor="Red">
</asp:RegularExpressionValidator>
<asp:Button ID="btnSubmit" runat="server" Text="Check Input" ValidationGroup="Submit" />

If you want to have it automatically validate when the textbox loses focus you can do that pretty easily, this is just a proof of concept.

 

On 1/11/2017 at 7:43 AM, TechguyES said:

an if statement would help

a quick search:http://stackoverflow.com/questions/8321871/how-to-make-a-textbox-accept-only-alphabetic-characters


private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]"))
    {
        MessageBox.Show("This textbox accepts only alphabetical characters");
        textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}

I don't recommend doing your validation *only* on the server side.  You should be checking it client side with a custom validator and then verifying on submit that there aren't any numbers in the text value.  Otherwise you're leaving open the possibility of the value being manipulated on submission and that's a security concern. 

 

edit: You also need to make sure you're accounting for punctuation in your regular expression so it's better to reject numbers than set it to accept letters.  Otherwise a period, a comma, or any other normal "language" type symbol will throw an error and you'll end up with a really unwieldy regular expression.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×