开发者

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

How to make a text-box accept only numeric charact开发者_StackOverflow社区ers (0-9) ? and give an error Message if it contains any alphabets or symbols.


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).


This MSDN article will help you:

  • How to: Create a Numeric Text Box


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.


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;
        }


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜