Search code examples
formssyntaxonkeypress

Using charcode to allow only numbers and enter key


I have this syntax:

<input onkeypress="return event.charCode >= 48 && event.charCode <= 57">

I use it on inputs to allow only numbers. But now i need to allow the enter key too since i can no longer use a button to submit the form(personal reasons).By now i found this:

  event.charCode = 13 // allows enter key

but i can't seem to find how to build the onkeypress syntax.Any help ? Thank you.


Solution

  • you can try:

    onkeypress="return event.charCode >= 8 && event.charCode <= 57"
    

    In this way you can allow enter key,numbers..and few other characters,except for alphabetic characters.I don't see any other way. Source: click