Question

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,

Was it helpful?

Solution

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

$('#test').keyup(function() {
   if ($(this).val() === '0')
   {
      $(this).val('');
   }    
});

OTHER TIPS

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()

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top