你怎么能允许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]+)?">
您可以使用带有一些正则表达式的常规<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
你应该只使用 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"/>
看看这是否可以帮助你。模式是正则表达式。查看正则表达式以获取更多自定义。
<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>
只允许数字、逗号和空格,如您在正则表达式中看到的那样