views:

80

answers:

3

Hi,

Basically, I do not want the user to enter '0' (zero) as the first character in a textbox which represents data with type of integer?

I would like to bind an event handler to handle this with jQuery.

Any experience?

Thanks,

A: 

you can use somthing like $('textbox').val().substr(0,1) != 0; Though it would be better if we knew what you would like accomplish

Sinan
+2  A: 

You can replace the value with the integer value if you want:

$(".IntegerInput").val(function(i, v) {
  return parseInt(v, 10);
});

This will parse the int and replace the value with it, removing any leading 0's.

Romuald made a god catch, for your specific case you'll need the radix argument on parseInt()

Nick Craver
Won't work as intended. For example `parseInt('0100')` returns 64. Just use `parseInt(v, 10)` and it will work fine
Romuald Brunet
@Romuald - Good catch, didn't think think about it defaulting leading 0 to a different base, updated
Nick Craver
+1  A: 

You could just simply replace it on the keyup like this:

$('#test').keyup(function() {
   if ($(this).val() === '0')
   {
      $(this).val('');
   }    
});
ryanulit
Wouldn't `if($(this).val() == '0') $(this).val('');` be easier? :)
Nick Craver
Yep, that makes it even easier. Got caught up in the keyup event arguments. I edited the answer, thanks.
ryanulit