Each time a character is entered, the whole input is matched with the RegExp to check validity. I try to make a RegEx for validating a form in javascript. Help identifying an arcade game from my childhood. var key = theEvent.keyCode || theEvent.which; One other issue: in IE up to and including version 8, arrays don't have an. Blazor Just just put in a bracket with ^ and add the comma. Oh yes! \D matches any non-digit. rev2023.7.13.43531. Change type="number" to type="text" because you can't put comma into number. If im applying for an australian ETA, but ive been convicted as a minor once or twice and it got expunged, do i put yes ive been convicted? It still seems to allow letters if you hold down the key. Instead, provide onscreen hints for format, validate input before sending and let users figure it out. Try this code: function isNumber(evt) { 0. GKFX May 27, 2020 at 10:18 Also below is the code that will allow only alphabets in a textbox. By using our site, you function isNumber (evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode (key); var regex = /^ [0-9.,]+$/; if (!regex.test (key)) { theEvent.returnValue = false; if (theEvent.preventDefault) theEvent.preventDefault (); } } Also, the former one doesn't allow pasting data, while later one does.