Failed_Noob
Failed_Noob

Reputation: 1357

How to make a text-box accept only numeric characters?

How to make a text-box accept only numeric characters (0-9) ? and give an error Message if it contains any alphabets or symbols.

Upvotes: 1

Views: 20883

Answers (6)

Jakir Hossain
Jakir Hossain

Reputation: 431

If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
    MessageBox.Show("Please enter numbers only")
    e.Handled = True
End If

Upvotes: 0

akkireddy
akkireddy

Reputation: 11

Here i am with pincode textbox name an pin code and ID as txtPinCode

you just write on top of the code under document function starts lets write this code and check once

$("#<%=txtPincode.ClientID%>").numeric();

thanks akreddy

Upvotes: 1

Developer
Developer

Reputation: 8646

How about this will allow only numeric data and Back space

Under Keypress event

 if ((!char.IsNumber(e.KeyChar)) && !(e.KeyChar == (char)Keys.Back))
        {
            e.Handled = true;
        }

Upvotes: 1

Erin Packard
Erin Packard

Reputation: 51

You could use a Regular Expression something like: ^(\d{0,11})(.\d{0,2})?$ This will allow 0-11 digits left of the decimal and 0-2 digits right of the decimal.

If you have Ajax Controls you could use a Filtered Textbox Extender <cc1:FilteredTextBoxExtender id="FilteredTextBoxExtender8" runat="server" TargetControlID="YourTextBox" FilterType="Custom,Numbers" ValidChars="." > </cc1:FilteredTextBoxExtender>

Or you could also try Marco's approach.

Upvotes: 1

Naveed
Naveed

Reputation: 42143

This MSDN article will help you:

Upvotes: 3

Marco
Marco

Reputation: 57593

You could catch the event KeyDown and check if e.KeyCode is numeric or not and discard it if you want.
Another thing you could try is using IsNumeric(txt.Text).

Upvotes: 4

Related Questions