Reputation: 293
How do I make a textbox in gridview accept numbers only without using validators? I am using C#, ASP.net.
I'm hoping to use KeyPress event or something.
Upvotes: 1
Views: 4812
Reputation: 9612
<asp:TextBox ID="txtUID" runat="server" CssClass="TextBox" onkeypress="return onlyNumbers(this);"/>
//Restrict the user to key-in chrectors and other special charectors
function onlyNumbers(evt) {
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
Reference:- CodeProject
Upvotes: 3