![](/img/trans.png)
[英]Allow 2 decimal places in <input type="number"> and comma every 3 digits
[英]How to allow input number type to have space and comma
你怎么能允许input type="number"
接受像1, 2, 3, 55
这样的东西,而不仅仅是带小数的常规number
。
我尝试使用模式,但它似乎并没有发挥它的魔力,因为它仍然只接受数字和小数。
Example: <input type="number" pattern="[0-9]+([\.,][0-9]+)?"> Desired: <input type="text" pattern="[0-9]+([\.,][0-9]+)?" value="1, 2, 3, 4">
<input type="number" pattern="[0-9]+([\.,][0-9]+)?">
您应该只使用 type=text 并使用 js 验证/格式化它。
$('input').on('change, keyup', function() { var currentInput = $(this).val(); var fixedInput = currentInput.replace(/[A-Za-z!@#$%^&*()]/g, ''); $(this).val(fixedInput); console.log(fixedInput); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text"/>
您可以使用带有一些正则表达式的常规<input type="text"/>
:
var input = document.querySelector('input'); input.addEventListener('input', function() { this.value = this.value.replace(/[^0-9 \,]/, ''); });
<input type="text"/>
正则表达式含义:
[^0-9 \,] # any character that's not a digit, a space, or a comma
看看这是否可以帮助你。 模式是正则表达式。 查看正则表达式以获得更多自定义。
<form>
<input type="text" pattern="([0-9]+.{0,1}[0-9]*,{0,1})*[0-9]" required>
<input type="submit">
</form>
我工作的 html 表单的一大块:
<div class="input-group input-group-auto col-auto">
<input class="form-control text-center"
oninput="this.value=this.value.replace(/[^0-9,]/g,'');"
type="text" id="telefonlar" name="telefonlar" required
/>
</div>
只允许数字、逗号和空格,正如您在正则表达式中看到的那样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.