I found this very short clean code to allow only numeric characters in a text box. Currently, it only covers numbers 0-9 and vice versa and deletes. I wanted it to also include the decimal / period, so I struggled with this by simply including the code 110 and / or 190. I can't get it to work. Can anyone see what I'm doing wrong?
$(document).ready(function() { $('input.numberinput').bind('keypress', function(e) { return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57) ) || (e.which!=110) ? false : true ; }); });
jsfiddle here : http://jsfiddle.net/justmelat/EN8pT/
HTML
<div class="label">Enter a number:</div> <input type="text" name="txtNumber1" id="txtNumber1" value="" class="numberinput" /> <div class="label">Enter a number:</div> <input type="text" name="txtNumber2" id="txtNumber2" value="" class="numberinput" /> </div>
source share