For example,to allow only digital numbers to be typed in:
<input type="text" id="target" />
For example,to allow only digital numbers to be typed in:
<input type="text" id="target" />
var value = $('#target').val();
function isNumber ( n ) { return !isNaN( n ) }
isNumber(value) // if its '3' then true
isNumber(value) // if 3 then true
isNumber(value) // if '' then false
Use the keypress()
event. This example prevents characters that aren't digits (0 = 48, 9 = 57).
$(function() {
$("#target").keypress(function(evt) {
if (evt.which < 48 || evt.which > 57) {
return false;
}
});
});
See this list of Javascript key codes.