How to remove the leading zero from number using jQuery?
jquery
Solution
"" + parseFloat(str)
Works out what the numerica value is, then makes it a string again. Bingo, no leading zeros.
Problem
I use this pattern to check for numbers only in a quantity field from 0 to whatever. how can I make the code not allow 012, 010.. etc I mean the starting zero, but still allow 0 as standalone number. 0, 1, 10, 1, etc. are ok, but not allow 012, 005, etc. Is there a pattern that does that? Here is my code but the problem with it, it removes the 0, even if it is not leading in number which I want to avoid. I want to allow the 0 as standalone number in my field. ``` $('.td-qnt input').live('change keyup blur', function(e) { var re = /^[0-9]\d*$/; var str = $(this).val(); $(this).val(str.replace(/^[ 0]/g,'')); if (re.test(str)){ var price = $(this).parent().parent().prev('td').html(); var realprice = price.replace(/[^0-9\.]/g,''); var result = realprice * str; var subtotal = result.toFixed(2); $(this).parent().parent().next('td').html('$'+subtotal); } else { $(this).parent().parent().next('td').html('$0.00'); $(this).val(''); } }); ```