1

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")
CriticalRebel
  • 63
  • 1
  • 7

1 Answers1

4

Note: 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>
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338