pencilCake
pencilCake

Reputation: 53243

How to ignore first entered character to be zero in a textbox with jQuery

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,

Upvotes: 0

Views: 1498

Answers (3)

ryanulit
ryanulit

Reputation: 5001

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

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

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630429

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

Upvotes: 2

Sinan
Sinan

Reputation: 5980

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

Upvotes: 0

Related Questions