There are numerous similar questions regarding this, but no answers are actually working. I would like to PREVENT (not validate afterwards) users from ENTERING more than 4 digits after the decimal point in an HTML input field. I can easily validate the input once the user navigates away from the field, but my intent is to actually PREVENT altogether the user from inputting more than 4 digits.
I am using the following code upon creating the input field:
text.setAttribute('step', '.0001')
text.setAttribute('placeholder', '00.0000')
text.addEventListener('focusout', () => this.validate(tId))
In my following focusout validate method:
validate (tId) {
let val = document.getElementById('text' + tId).value
let txtInput = document.getElementById('text' + tId)
// Validation
}
I have tried the following but without succes:
if (val.match('^\\d+(\\.\\d{1,4})?$')) {
}
And
val = parseFloat(val).toFixed(4)
But these are not working as I wish. They do properly validate the input once navigating away from the field, but do not actually PREVENT the user from entering more than 4 digits.