3

I have a price field that only allows the use of numbers. I use the following code in the head:

jQuery(document).ready(function($) {
    jQuery('#item_price').keyup(function () { 
       this.value = this.value.replace(/[^0-9]/g,'');
    });
});

and this is my field:

<input type="text" minlength="2" id="item_price" name="item_price">

What i'm trying to do now is force the field to be empty if the person types in 0 or 00 or 000 and so on... but without messing up with numbers that contain 0 but are actually a specific price (for example 300, 10250, 10).

Is there any way that i can accomplish this?

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
Gman
  • 783
  • 2
  • 8
  • 24

3 Answers3

4

Try checking if the value is the number 0.

if(parseInt(this.value, 10) === 0){
    this.value = '';
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Does this work for you?

jQuery('#item_price').keyup(function () { 
  this.value = this.value.replace(/[^0-9]/g,'');
  this.value = this.value.replace(/^[0]+/g,'');
});​

DEMO

kei
  • 20,157
  • 2
  • 35
  • 62
0
jQuery(document).ready(function() {
    jQuery('#item_price').keyup(function() {
        var $this = $(this);
        if (/^0+$/.test($this.val()) {
            $this.val('');
        }
    });
});
jmar777
  • 38,796
  • 11
  • 66
  • 64