[英]How to allow only numeric input, allow 2 digits after decimal and allow to input minus in jquery?
I'd like to restrict users input in a form to a decimal with a maximum of two numbers after (.) And users allow to input minus.我想将表单中的用户输入限制为在 (.) 后最多有两个数字的十进制数,并且用户允许输入减号。 and a text field which only accepts a number.和一个只接受数字的文本字段。
<input type="text" name="debet[]" placeholder="debet" class=" money" value="0"/>
Script:脚本:
$('body').on('focus',".money", function(){
$(this).simpleMoneyFormat();
});
Here is a jsFiddle example这是一个 jsFiddle 示例
Somebody can help me with this?有人可以帮我解决这个问题吗?
<input type="number" name="debet[]" placeholder="debet" class="money"/>
var timer;
$('input').keyup(function (){
clearTimeout(timer);
timer = setTimeout(function (event) {
$('input').val(Number($('input').val()).toFixed(2))
}, 1000);
});
this should do, it will fire the event every 1 seconds这应该可以,它会每 1 秒触发一次事件
https://jsfiddle.net/usdf1nx6/ https://jsfiddle.net/usdf1nx6/
You can add an event listener for blur
event on input.您可以为输入的blur
事件添加事件侦听器。 And, then use a regex to validate the input using RegExp.test()
method然后,使用正则表达式使用RegExp.test()
方法验证输入
let input = document.querySelector('input'); // input element
const checkRegex = /^\-?\d[\,\d]+\d\.\d{0,2}$/; // regex for the format: DDD,DDD.DD
input.addEventListener('blur', evt => {
if(checkRegex.test(input.value)) {
input.style.borderColor = '#080'; // indicates correct input
} else {
input.style.borderColor = '#F46'; // indicates wrong input
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.