Saturday, August 31, 2013

How do make the texbox accept only numerics, del key and backspace?

I always prefer to do it via client side Javascript... It is faster than using code behind



<asp:TextBox ID="txtNumber" runat="server" onkeypress="AllowNumeric();" MaxLength="8"></asp:TextBox>

Choose the Javascript you prefer.



function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function AllowNumeric() {
if ((event.keyCode > 47 && event.keyCode < 58) || (event.keyCode == 46) || (event.keyCode == 45)) {
event.returnValue = true;
}
else {
event.returnValue = false;
}
}


No comments:

Post a Comment