Shaun
Shaun

Reputation: 811

Input number field more than 0?

Is it possible to set a minimum on a input number field so it's more than 0 but the user can still enter 0.01 for example?

I've tried this but it hasn't worked..

<input required="" name="amount" type="text" min="0.01" value="0.00" />

Upvotes: 2

Views: 17514

Answers (4)

Dalin Huang
Dalin Huang

Reputation: 11342

A simple solution could be, add an input event to your . I'm given an id to that input. If the input less than 0.01 then reset it.

Also to use step to control the precision.

Also, a situation could be input = 0.0111, so in the else statement we do this: Math.round(+this.value * 100)/100; (+ will force the variable to become number type and use Math.round to get the input rounded to two decimal)

document.getElementById('test').addEventListener('change', function (e) {
    if(this.value < 0.01){
      this.value = '';
    } else {
      this.value = Math.round(+this.value * 100)/100;
    }
});
<input required="" id="test" name="amount" type="number" step="0.01" value="0.00" />

Upvotes: 0

Jaydeep Mor
Jaydeep Mor

Reputation: 1703

Try this.

<input required="" name="amount" id="amount" type="number" min="0.01" 
value="0.00" onblur="return validate(this);" />
    <script type="text/javascript">
function validate(amount){
    if(parseFloat(amount.value)<=0){
        amount.value = "";
        return false;
    }
    return true;
}
</script>

Upvotes: 0

Stanislav Kvitash
Stanislav Kvitash

Reputation: 4622

Use type="number" with min="0" (you can also use step="0.01" attribute to limit the increments at which value can be set):

<input required="" name="amount" type="number" min="0" step="0.01" value="0.01" />

Upvotes: 3

Dmitry
Dmitry

Reputation: 2087

In you code type="text" Attribute min available for number, range & date types. Try this:

<input required="" name="amount" type="number" min="0.01" value="0.01" />

Upvotes: 0

Related Questions