admin管理员组文章数量:1356860
Why does the input field allow alphabetic characters and not restrict the input to only numbers?
<input type="tel" id="phone" name="phone_number" placeholder="555-555-5555" pattern="\d{3}-\d{3}-\d{4}">
Why does the input field allow alphabetic characters and not restrict the input to only numbers?
<input type="tel" id="phone" name="phone_number" placeholder="555-555-5555" pattern="\d{3}-\d{3}-\d{4}">
Share
Improve this question
asked Sep 25, 2021 at 19:38
BrendoBrendo
711 silver badge4 bronze badges
3
- 3 type=tel is only used to bring up the numpad on supported devices and browsers. It does not perform any validation / restriction on user input – Manos Kounelakis Commented Sep 25, 2021 at 19:45
- Maybe for country code (+44) and area code (444) syntax. – HelpingHand Commented Sep 25, 2021 at 19:45
- 1 It's all explained in the documentation. – Andy Commented Sep 25, 2021 at 19:45
3 Answers
Reset to default 5It does not restrict the types of characters that can be input, but browsers will prevent submission if the input value does not match your pattern.
If you do not want to allow certain character inputs, you can use JavaScript to remove those characters: onkeyup="this.value = this.value.replace(/[^0-9-]/g, '');"
.
input:invalid {
color: red;
}
<form>
<input type="tel" id="phone" name="phone_number" placeholder="555-555-5555" pattern="\d{3}-\d{3}-\d{4}" onkeyup="this.value = this.value.replace(/[^0-9-]/g, '');">
<input type="submit" value="Submit">
</form>
https://developer.mozilla/en-US/docs/Web/HTML/Element/input/tel
Andy's link to the documentation had the answer I was looking for.
the input value is not automatically validated to a particular format before the form can be submitted, because formats for telephone numbers vary so much around the world
Because many country use char in the phone numbers like ’+-()’ space etc.
本文标签: javascriptWhy does inputtype39tel39 allow nonnumerical characters to be enteredStack Overflow
版权声明:本文标题:javascript - Why does input[type='tel'] allow non-numerical characters to be entered? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744005355a2574601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论