• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 722
  • Last Modified:

only allow numeric values on textbox

Hi,

how can we only allow numeric values on textbox?

Thanks.
0
lynnton
Asked:
lynnton
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
ayha1999Commented:
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
0
 
tusharashahCommented:
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
0
 
laotzi2000Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lynntonAuthor Commented:
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
0
 
ayha1999Commented:
Hi,,
my post will accept only numeric values.

try and see.

ayha
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
tusharashahCommented:
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;")
0
 
tusharashahCommented:
Also, remove one Extra "(" from JavaScript code...

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now