Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent input field from typing numbers on Firefox

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try adding this to your input tag.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading