Friday, August 21, 2009

Allow to enter only numbers in textbox

At times there is a need when we want to allow users to enter only number.
Say when its phone number, instead of validation after submit we can restrict users to enter non numeric characters using the following script:

function isNumberKey(evt)
var charCode = (evt.which) ? evt.which : event.keyCode;
if(!((charCode>=48&&charCode<=57)|| (charCode==46) || (charCode==8))) return false; return true; } To use it in HTML: <input type="text" onkeypress="return isNumberKey(event);" />

1 comment: