I am looking to implement a restriction on the total number of input digits to 3. Users should be able to enter numbers like 333
, 123
, etc. However, if they include a decimal point, they should only be allowed to enter 2 digits after the decimal point. Valid inputs include:
123
123.33
Below is the code snippet I have developed so far:
isNumber (event, message) {
// handling multiple decimal points
if (!/\d/.test(event.key) && (event.key !== '.' || /\./.test(message))) {
return event.preventDefault()
}
// enforcing limit of three digits before and two digits after the decimal point
if (/^(\d{0,3}\.)?\d{1,2}$/.test(message)) return event.preventDefault()
}
However, the regex pattern, /^(\d{0,3}\.)?\d{1,2}$/
, is not producing the desired results in my scenario.