We can disable the keys to enter inside the textbox. For example if we have a textbox for entering user's age then a user only need to enter numbers (The age will only be in number format ie 12,18 and so on)
We can use the following javascript to disable rest of the keys inside the textbox using the ASCII codes of the characters:-
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
It will pick up the ASCII which lies only between 48 to 57(0-9). We can call them on textbox by the following way:-
<input name="txtNumbers" type="text" id="txtNumbers" runat="server" onkeypress="return isNumberKey(event)" placeholder="Enter numbers only" />
0 Comment(s)