Link to home
Start Free TrialLog in
Avatar of Anthony Matovu
Anthony MatovuFlag for Uganda

asked on

Limiting entry to numeric characters for a textbox

I want to limit entry into a text box to numeric characters (vb.net)
Avatar of bitref
bitref
Flag of United States of America image

1) Declare a variable to hold JavaScript for enabling only Numbers as follows:
Dim strNumberOnlyValidation As String = "if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;"

Open in new window


2) Link the above JavaScript Variable to the requested TextBox as follows in Page_Load:
MyTextBox.Attributes("onKeypress") = strNumberOnlyValidation

Open in new window

You may find a list of JavaScript KeyCodes here:
Avatar of Kiran Sonawane
Like this

<asp:TextBox runat="server" id="txtNumber" />
<asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="^[0-9]+$" errormessage="Please enter digit only!" />

OR

<asp:TextBox runat="server" id="txtNumber" />
<asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtNumber" validationexpression="\d+" errormessage="Please enter digit only!" />
Try This:
<asp:RegularExpressionValidator runat="server" id="rexNumber"
            controltovalidate="txtSN1" validationexpression="(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)" errormessage="Please enter digit only!" />

http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
Avatar of Anthony Matovu

ASKER

Sorry, it is a windows application, not an asp.net application
thank you
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial