[英]How to apply space in value of <input type=“number”>?
I am trying to add space in <input type="number">
value in html.我正在尝试在 html 的
<input type="number">
值中添加空格。
But whenever i add space in value like this value="0401 889 889"
than there is nothing appear in <input type="number">
.但是,每当我在
value="0401 889 889"
这样的值中添加空间时, <input type="number">
中就没有任何内容出现。
Without adding a blank space不加空格
If i am not add blank space in <input type="number">
's value than its works fine.如果我没有在
<input type="number">
的值中添加空格而不是它的工作正常。
<input type="number" value="0401889889">
After adding a blank space in ``在 `` 中添加一个空格后
<input type="number" value="0401 889 889">
What i exactly want我到底想要什么
<h2>Without adding a space in value</h2> <input type="number" value="0401889889"> <h2>With adding a space in value</h2> <input type="number" value="0401 889 889">
Instead, you can use this相反,您可以使用这个
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
Check this answer: https://stackoverflow.com/a/25121813/14050736检查这个答案: https://stackoverflow.com/a/25121813/14050736
You'll have to modify the regex a bit according to the group of number you desire您必须根据您想要的数字组稍微修改正则表达式
Edit: I just noticed: Your input must be in "text" not "number"编辑:我刚刚注意到:您的输入必须是“文本”而不是“数字”
If you want to prevent users from typing alphabetic characters you need to set an event listener for the "input" event that delete invalid characters from the value on-the-fly:如果您想阻止用户输入字母字符,您需要为“输入”事件设置一个事件侦听器,以便从值中即时删除无效字符:
const phone = document.getElementById('phone'); phone.addEventListener('input', (event) => { // delete any character that is not a digit or a space event.target.value = event.target.value.replace(/[^0-9^ ]/gi, ''); }); // or, if you have more than one input of type "tel" (eg mobile and landline) const phones = document.querySelectorAll('input[type=tel]'); phones.forEach(phone => phone.addEventListener('input', (event) => { event.target.value = event.target.value.replace(/[^0-9^ ]/gi, ''); }))
<input type="tel" id="phone" name="phone" placeholder="0401 889 889" pattern="[0-9]{4} [0-9]{3} [0-9]{3}"> <input type="tel" id="mobile" name="mobile" placeholder="0401 998 998" pattern="[0-9]{4} [0-9]{3} [0-9]{3}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.