Text box to accept only number






4.13/5 (19 votes)
Text box to accept only number
<HTML> <HEAD> <SCRIPT language=Javascript> function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } </SCRIPT> </HEAD> <BODY> <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar"> </BODY> </HTML>Just paste the
Script
Section in the Head
section and in on key press call the function.
For C#, just paste the following code:
function allownumbers(e) { var key = window.event ? e.keyCode : e.which; var keychar = String.fromCharCode(key); var reg = new RegExp("[0-9.]") if (key == 8) { //alert(key); keychar = String.fromCharCode(key); }And in
PageLoad
:
txtNum.Attributes.Add("onkeypress", "javascript:return allownumbers(event);");