Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 : Input field with the type of 'number' will still accept higher value than its 'max' using keyboard inputs

Hello guys need some help here. i want to have limit the numbers inputted in my input field by putting max attribute to it. i have no problem with that until i use my keyboard to input data on it. seems like the max attribute is not filtering the input coming from the keyboard.

e.g

<input type="number" max="5" />

i can't go until 6 using the up and down arrow but when i manually put 6 using keyboard it's accepts it. how can i prevent? thank you

like image 555
Arnold wanna be Avatar asked Apr 17 '17 07:04

Arnold wanna be


People also ask

How do I limit a number into an input type number?

Complete HTML/CSS Course 2022 To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”

Is number a valid input type in html5?

With the number input type, you can constrain the minimum and maximum values allowed by setting the min and max attributes. You can also use the step attribute to set the increment increase and decrease caused by pressing the spinner buttons. By default, the number input type only validates if the number is an integer.

What does an html5 type attribute for an input element do?

The type attribute specifies the type of <input> element to display. If the type attribute is not specified, the default type is "text".


2 Answers

You would need to use JavaScript to do it. This will not let the user enter a number higher than 5:

 <input type="number" max="5" onkeyup="if(this.value > 5) this.value = null;">

Another possible solution is to completely block the keyboard input by replacing onkeyup=".." event in the code above with onkeydown="return false".

like image 73
Ilia Avatar answered Oct 20 '22 04:10

Ilia


have no problem with that until i use my keyboard to input data on it. seems like the max attribute is not filtering the input coming from the keyboard

This is how HTML5 validation/constraint work. However, it will invalidate when the form submits. Alternatively, you can validate it yourself. To validate yourself, you need to wire up Javascript and call the checkValidity() on the input element.

checkValidity() of the constraints API will check the validity state of the element and will return the state of whether the input element validate or not. This will also set the validity object on the input so that you can query more details.

Ref: https://html.spec.whatwg.org/multipage/forms.html#constraints and https://html.spec.whatwg.org/multipage/forms.html#form-submission-algorithm

You can also use the :invalid selector in CSS to highlight invalid inputs.

Example Snippet:

var input = document.getElementById('test'), 
    result = document.getElementById('result');
  
input.addEventListener('blur', validate);

function validate(e) {
  var isValid = e.target.checkValidity();
  result.textContent = 'isValid = ' + isValid;
  if (! isValid) {
    console.log(e.target.validity);
  }
}
input[type=number]:invalid {
  border: 2px solid red;
  outline: none;
}
<label>Enter value and press tab: </label><br/>
<input id="test" type="number" min="1" max="10" />
<hr/>
<p id="result"></p>
like image 41
Abhitalks Avatar answered Oct 20 '22 04:10

Abhitalks