-1

I want to make a textbox that allows only numeric values to be entered. how to achieve this?

Pradeep Kesharwani
  • 1,480
  • 1
  • 12
  • 21
Nirav Darji
  • 43
  • 1
  • 1
  • 8

5 Answers5

4

use RegularExpressionValidator

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>

How to allow only integers in a textbox?

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
2

In ASP.NET try this:

<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer" 
 ControlToValidate="TxtBox" ErrorMessage=Error" />
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You could use the ASP.NET Regular Expression Validator and write a RegEx that only accepts numeric values.

If you want to intercept keystrokes, so that only numerical keys are entered, you'll have to use JavaScript (I wouldn't do that).

Alexander
  • 2,457
  • 1
  • 14
  • 17
0

dear you can do this by jquery try this if you are using jquery

$(document).ready(function() {
$("#txtboxToFilter").keydown(function (e) {
    // Allow: backspace, delete, tab, escape, enter and .
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
         // Allow: Ctrl+A
        (e.keyCode == 65 && e.ctrlKey === true) || 
         // Allow: home, end, left, right
        (e.keyCode >= 35 && e.keyCode <= 39)) {
             // let it happen, don't do anything
             return;
    }
    // Ensure that it is a number and stop the keypress
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
});
});
Developerzzz
  • 1,123
  • 1
  • 11
  • 26
0

try below code

<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationGroup="VGrpSave" SetFocusOnError="false" Display="None" ErrorMessage="Only numeric values are allowed" ControlToValidate="txtID" ValidationExpression="\d*"></asp:RegularExpressionValidator>
<asp:Button ID="btnSubmit" runat="server" ValidationGroup="VGrpSave" Text="Submit"></asp:Button>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" ShowMessageBox="True" ShowSummary="false" ValidationGroup="VGrpSave" />
Purvesh Desai
  • 1,797
  • 2
  • 15
  • 39