0

In reference to this question: HTML Text Input allow only Numeric input

with one possible solution:

 
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>

I wonder if it's also possible to have this solution (I'm looking for a one line statement, nothing else) adapted, such that it permits, besides for all integers, also for the delete key (and possibly backspace key) to be able to be pressed? So that a number which was inserted can be erased again.

Many thx!

Community
  • 1
  • 1
Tim
  • 125
  • 3
  • 12
  • The solution is already on the link you posted (though it is in JQuery, which sucks a bit). It can't be a single statement, and it shouldn't. – Domino Nov 15 '15 at 21:18
  • Note that the "solution" doesn't prevent entering values other than numbers, so doesn't work in that regard. – RobG Nov 15 '15 at 23:09
  • @RobG Thx for the comment, but how do u conclude that? For me, using this, I can't insert anything else than integers between 0 and 9? – Tim Nov 16 '15 at 13:42
  • @Tim—you can paste any value you like, or drag text into the input. – RobG Nov 16 '15 at 22:41
  • @RobG True I haven't checked that, thx! Anyway, this "solution" is good enough for my purposes. – Tim Nov 17 '15 at 08:55

1 Answers1

0

In some browsers, Backspace and Delete will not trigger onkeypress. In other browsers, backspace and delete have charCode 0, so try this:

<input type="text" onkeypress='return !event.charCode || (event.charCode >= 48 && event.charCode <= 57)'/>
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32