![](/img/trans.png)
[英]How to make Vue js vuetify input allow only 3 digits before decimal and 2 digits after decimal
[英]How to allow only numeric input, allow 2 digits after decimal and allow to input minus in jquery?
我想將表單中的用戶輸入限制為在 (.) 后最多有兩個數字的十進制數,並且用戶允許輸入減號。 和一個只接受數字的文本字段。
<input type="text" name="debet[]" placeholder="debet" class=" money" value="0"/>
腳本:
$('body').on('focus',".money", function(){
$(this).simpleMoneyFormat();
});
有人可以幫我解決這個問題嗎?
<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);
});
這應該可以,它會每 1 秒觸發一次事件
您可以為輸入的blur
事件添加事件偵聽器。 然后,使用正則表達式使用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.