lynnton
asked on
only allow numeric values on textbox
Hi,
how can we only allow numeric values on textbox?
Thanks.
how can we only allow numeric values on textbox?
Thanks.
Put this in your Page_Load event, it will allow you to Input only numbers in TextBox..
TextBox1.Attributes.Add("o nKeyPress" ,"if ( (event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;");
-tushar
TextBox1.Attributes.Add("o
-tushar
You should add a regularexpression validator for your textbox
with a regular expression \d+
Or you can derive your own control from textbox and only allow
input numbers
with a regular expression \d+
Or you can derive your own control from textbox and only allow
input numbers
ASKER
Server Error in '/' Application.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30037: Character is not valid.
Source Error:
Line 8: 'conencting to db and binding
Line 9: Sub Page_Load(Sender As Object, E As EventArgs)
Line 10: txtoperatorid.Attributes.A dd("onKeyP ress","if ( (event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;");
Line 11: If Not Page.IsPostBack Then
Line 12: label1.text=""
Source File: c:\inetpub\wwwroot\opermas .aspx Line: 10
--------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30037: Character is not valid.
Source Error:
Line 8: 'conencting to db and binding
Line 9: Sub Page_Load(Sender As Object, E As EventArgs)
Line 10: txtoperatorid.Attributes.A
Line 11: If Not Page.IsPostBack Then
Line 12: label1.text=""
Source File: c:\inetpub\wwwroot\opermas
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are using VB.Net you need to remove ";" from the line that I have posted, so include following line:
txtoperatorid.Attributes.A dd("onKeyP ress","if ( (event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;")
txtoperatorid.Attributes.A
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to allow for any number value, not just integers,
the simplest way is to use a regular expression validator.
But you have to change the validation expression though.
the simplest way is to use a regular expression validator.
But you have to change the validation expression though.
You can use range validator and force user to enter only numbers. You can also set min. and max. values allowed. If don't want requiredfieldvalidator you can remove it.
<asp:textbox id="YourID" runat="server" Width="40px" ></asp:textbox>
<asp:requiredfieldvalidato
<asp:rangevalidator id="YourID" runat="server" ErrorMessage="Minimum 0 required." ControlToValidate="YourID"
hope this helps.
ayha