Link to home
Start Free TrialLog in
Avatar of lynnton
lynnton

asked on

only allow numeric values on textbox

Hi,

how can we only allow numeric values on textbox?

Thanks.
Avatar of ayha1999
ayha1999

Hi,

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:requiredfieldvalidator id="ID" runat="server" ErrorMessage="Value required." ControlToValidate="YourID" ></asp:requiredfieldvalidator>
<asp:rangevalidator id="YourID" runat="server" ErrorMessage="Minimum 0 required." ControlToValidate="YourID" MaximumValue="999" MinimumValue="0" Type="Integer">*</asp:rangevalidator></TD>

hope this helps.

ayha
Put this in your Page_Load event, it will allow you to Input only numbers in TextBox..

TextBox1.Attributes.Add("onKeyPress","if ( (event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;");

-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
Avatar of lynnton

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.Add("onKeyPress","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
SOLUTION
Avatar of ayha1999
ayha1999

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
Avatar of Éric Moreau
If you are using VB.Net you need to remove ";" from the line that I have posted, so include following line:

txtoperatorid.Attributes.Add("onKeyPress","if ( (event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;")
ASKER CERTIFIED SOLUTION
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
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.