Home HTML text input allows only numeric input
Reply: 0

HTML text input allows only numeric input

user9762
1#
user9762 Published in September 19, 2018, 6:43 pm

Is there a quick way to set an HTML text input (<input type=text />) to only allow numeric keystrokes (plus '.')?

share|improve this question
  • 72
    Many solutions here only work when keys are pressed. These will fail if people paste text using the menu, or if they drag and drop text into the text input. I've been bitten by that before. Be careful! – Bennett McElwee Jan 24 '11 at 21:09
  • 73
    @JuliusA - you always always need server-side validation anyway. – Stephen P Nov 23 '11 at 1:57
  • 33
    <input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input> – Droogans Jan 20 '13 at 20:13
  • 13
    @Droogans notice that also disables any other key, like TAB to go to the next input or any other shortcut not directly involved with input like cmd+R for refreshing the website if the input is focused. – Alejandro Pérez Nov 5 '13 at 9:54
  • 1
    If you are okay with Plugin, use NumericInput. Demo: jsfiddle.net/152sumxu/2 More details here stackoverflow.com/a/27561763/82961 – Faiz Dec 19 '14 at 7:53

57 Answers 57

active oldest votes
1 2 next
up vote 906 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.497517 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO