I have an input box:
<input class="longboxsmall" type="text" name="number" value=""/> And I want to disallow the number 0 being the first number entered in the box how can I do this??? Can I use Jquery??
$(".longboxsmall").keyup(function(){ var value = $(this).val(); value = value.replace(/^(0*)/,""); $(this).val(value); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="longboxsmall" type="text" name="number" value=""/> JS
$(".longboxsmall").keyup(function(){ var value = $(this).val(); value = value.replace(/^(0*)/,""); $(this).val(value); }); HTML
<input class="longboxsmall" type="text" name="number" value=""/> Try in jsfiddle.
IT's good to intercept another events, like blur, change, focusout, etc. See jquery. Tks @Simen Echholt.
Explanation:
$(".longboxsmall").keyup
Jquery take every element with class longboxsmall and are ready to call a function on onkeyup event.
var value = $(this).val(); value = value.replace(/^(0*)/,""); $(this).val(value); This function uses a regex to replace every 0 in begin (^) of value.
mousedown/up and/or changebest way is prevent typing 0 in the first position while user is typing. we can achieve this by to use keyup event. by that we will avoid many case's like.
its prevent copy paste like 0123.
$(document).on('keyup','#TextBoxID', function(event){ if($(this).val().match(/^0/)){ $(this).val(''); return false; } }); $(".longboxsmall").keyup(function(){ var value = $(this).val(); value = value.replace(/[^(1*)]+|[^(0*)]+/,""); $(this).val(value); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="longboxsmall" type="text" name="number" value=""/>