I want to make a textbox that allows only numeric values to be entered. how to achieve this?
Asked
Active
Viewed 2.8k times
5 Answers
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>

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
-
Why JQUERY? OP has not tagged it! – Rahul Tripathi Mar 11 '14 at 07:48
-
dear its a suggestion if its good for him then he will tak it as i ask in my answer if you use jquery then use it – Developerzzz Mar 11 '14 at 07:49
-
Thank you. I don't have knowledge of Jquery, I'm Just Novice to programming with having prior knowledge of C#, Sql Server. though I Appreciate your help. – Nirav Darji Mar 11 '14 at 07:50
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