最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get input of exact length? - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

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>

发布评论

评论列表(0)

  1. 暂无评论