How to set the input value of a text field to be exact? I want to get a document number of exactly 10 digit from user. The following two does not work at the same time:
var input = document.createElement("input");
input.setAttribute("min","10")
input.setAttribute("max","10")
How to set the input value of a text field to be exact? I want to get a document number of exactly 10 digit from user. The following two does not work at the same time:
var input = document.createElement("input");
input.setAttribute("min","10")
input.setAttribute("max","10")
Share
Improve this question
asked Mar 22, 2019 at 21:37
CriticalRebelCriticalRebel
631 silver badge10 bronze badges
2
- Possible duplicate of Is there a minlength validation attribute in HTML5? – Gonzalo.- Commented Mar 22, 2019 at 21:40
-
I'd use
<input type="text" pattern="\d{10}" title="10-digit number" />
– Niet the Dark Absol Commented Mar 22, 2019 at 21:42
1 Answer
Reset to default 5Note: min/max are only valid on type="number"
or type="date"
inputs.
This can be done with pure html. You can set the field to type="number"
and require the min value to be 1000000000
(10 digits) and a max value of 9999999999
(also 10 digits). Then just tack on the required
parameter on the field. The form won't submit unless it validates due to the required
parameter.
<form action="">
<input type="number" min="1000000000" max="9999999999" required>
<input type="submit" value="send">
</form>
Another thing we can do is use a pattern
on a text input
with required
:
<form action="">
<input type="text" pattern="[0-9]{10}" required>
<input type="submit" value="send">
</form>