Prevent negative inputs in form input type=“number

2019-01-16 14:26发布

I want to restrict user input to positive numbers in an html form.

I know you can set min="0", however it is possible to bypass this by manually entering a negative number.

Is there any other way to solve this without writing a validation function?

5条回答
成全新的幸福
2楼-- · 2019-01-16 15:02

This uses Javascript, but you don't have to write your own validation routine. Instead just check the validity.valid property. This will be true if and only if the input falls within the range.

<html>
<body>
<form action="#">
  <input type="number" name="test" min=0 oninput="validity.valid||(value='');"><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

查看更多
再贱就再见
3楼-- · 2019-01-16 15:06

The following script will only allow numbers or a backspace to be entered into the input.

var number = document.getElementById('number');

number.onkeydown = function(e) {
    if(!((e.keyCode > 95 && e.keyCode < 106)
      || (e.keyCode > 47 && e.keyCode < 58) 
      || e.keyCode == 8)) {
        return false;
    }
}
<input type="number" id="number" min="0">

查看更多
走好不送
4楼-- · 2019-01-16 15:10

If you want to ensure default value, i.e min value, or any other value, this is working solution. This is also preventing to clear the input field. Same way you can set to it's max value as well.

<input type="number" min="1" max="9999" maxlength="4" oninput="this.value=this.value.slice(0,this.maxLength||1/1);this.value=(this.value   < 1) ? (1/1) : this.value;">

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-16 15:13

type="number" already solves allowing numbers only to be typed as the other answers here point out.

Just for reference: with jQuery you can overwrite negative values on focusout with the following code:

$(document).ready(function(){
    $("body").delegate('#myInputNumber', 'focusout', function(){
        if($(this).val() < 0){
            $(this).val('0');
        }
    });
});

This does not replace server side validation!

查看更多
疯言疯语
6楼-- · 2019-01-16 15:14

This is not possible without validating the value of the input.

input type=number

The input element with a type attribute whose value is "number" represents a precise control for setting the element’s value to a string representing a number.

Since it is a string representing the number there is no way to be sure that string may be representing numeric values or not.

The Permitted attributes will not give you the ability to validate the value of the number input control.

One way to do this with the help of JavaScript could look like this.

// Select your input element.
var numInput = document.querySelector('input');

// Listen for input event on numInput.
numInput.addEventListener('input', function(){
    // Let's match only digits.
    var num = this.value.match(/^\d+$/);
    if (num === null) {
        // If we have no match, value will be empty.
        this.value = "";
    }
}, false)
<input type="number" min="0" />

If you are planing on sending your data to a server make sure to validate the data on the server as well. Client side JavaScript can not ensure that the data that is being sent will be what you expect it to be.

查看更多
登录 后发表回答