javascript - Input box - Prevent user from entering 0 as first digit -
i have 2 input boxes. want user should not able enter digit starts 0. note 0 @ beginning. can @ end or middle not @ beginning.
so allow 4, 40, 4440 not 04.
check fiddle i've put several other restrictions numeric values allowed, length 4. want add restriction related 0.
how can done using jquery?
html
<input name="major" size="1" value="4" class="major_rev">. <input name="minor" size="1" value="24" class="minor_rev">
try this
$(document).ready(function() { //for numeric $(".major_rev").keydown(function(event) { // allow backspace , delete if ( event.keycode == 46 || event.keycode == 8) { // let happen, don't } else { // ensure number , stop keypress if ((event.keycode !==9) && (event.keycode < 48 || event.keycode > 57 )) { event.preventdefault(); } else{ if($.trim($(this).val()) =='') { if(event.keycode == 48){ event.preventdefault(); } } } } }); $(".minor_rev").keydown(function(event) { // allow backspace , delete if ( event.keycode == 46 || event.keycode == 8 ) { // let happen, don't if($.trim($(this).val()).length==0) { if(event.keycode==48){ event.preventdefault(); } } } else { // ensure number , stop keypress if (event.keycode < 48 || event.keycode > 57 ) { event.preventdefault(); } else{ if($.trim($(this).val()) =='') { if(event.keycode == 48){ event.preventdefault(); } } } } }); //for maxlength $(".major_rev").prop("maxlength","4"); $(".minor_rev").prop("maxlength","4"); });
Comments
Post a Comment