Home > database >  How to prevent input field from typing numbers on Firefox
How to prevent input field from typing numbers on Firefox

Time:07-05

I have a bug in my application. It's a currency converting app - and it should only accept numbers.

I wrote an application that takes numbers from input. It works perfectly fine on Chrome, but firefox allows me/user to type in any character. I tried all kinds of methods, and functions to prevent this behavior - such as regex functions with tests to see if e.target.value is a number or not, pattern="[0-9]", inputype='numeric' and a few more.

<input type="number" onChange={e => setFromTo(e.target.value)} />

Does anybody know how to prevent firefox input field from accepting any character, and only taking in numbers?

CodePudding user response:

Try adding this line in your input field

onKeyPress={(event) => {(!/[0-9]/.test(event.key)) {
                            event.preventDefault();
                          }
                        }}

CodePudding user response:

try adding this to your input tag.

oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');"
  • Related