javascript - Restricting an input box to only numbers 0-9 -


this question has answer here:

how can 1 restrict inputbox use numbers only.

so if user type in a-z, not work in inputbox.

to may seem easy me, sounds rocket science.

no jquery please.

<!doctype html>  <html>  <head>  </head>  <body>  <input type="text" id="numbersonly" />  </body>  </html> 

you use input html5 number type

<input type="number" /> 

or javascript, input number type doesn't really limit input numbers :

document.getelementbyid('numbersonly').addeventlistener('keydown', function(e) {     var key   = e.keycode ? e.keycode : e.which;      if (!( [8, 9, 13, 27, 46, 110, 190].indexof(key) !== -1 ||          (key == 65 && ( e.ctrlkey || e.metakey  ) ) ||           (key >= 35 && key <= 40) ||          (key >= 48 && key <= 57 && !(e.shiftkey || e.altkey)) ||          (key >= 96 && key <= 105)        )) e.preventdefault(); }); 

fiddle


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -