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();
});
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
You can add an event listener for blur
event on input. And, then use a regex to validate the input using RegExp.test()
method
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
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.