简体   繁体   中英

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" />

使用type="number" with min="0" (你也可以使用step="0.01"属性来限制可以设置值的增量):

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

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" />

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" /> 

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM