How to delete content in input field using jquery if value is 0, 00, 000 and so on?

I have a price field that allows only numbers to be used. I use the following code in my 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 dials 0 or 00 or 000, etc ... but without errors with numbers that contain 0, but are actually a specific price (for example 300 , 10250, 10).

Is there a way I can do this?

+3
source share
3 answers

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

+1
source

Try checking if the value is a number 0.

if(parseInt(this.value, 10) === 0){
    this.value = '';
}
+4
source
jQuery(document).ready(function() {
    jQuery('#item_price').keyup(function() {
        var $this = $(this);
        if (/^0+$/.test($this.val()) {
            $this.val('');
        }
    });
});
0
source

All Articles