1

I am using following code in my-eclipse IDE.

<html>
    <head>
        <script>
            function isNumberKey(evt){
                var charCode = (evt.which) ? evt.which : evt.keyCode;
                if (charCode > 31 && (charCode < 48 || charCode > 57)){
                    return false;
                    return true;
                }
            }
        </script>
    </head>
    <body>
        <input name="form_number" onkeypress="return isNumberKey(event)" type="text" maxlength="4">
    </body>
</html>

This code does not return the proper output when testing on my built-in browser in my IDE.

I tried to run this snippet on IE (thus using an external browser) and everything seems to work just fine. I feel like there are not mistakes on my code, but I might be wrong. Is my code wrong or is this issue browser related ?

Ajay Makwana
  • 2,330
  • 1
  • 17
  • 32
Santosh
  • 111
  • 2
  • 10

2 Answers2

0

Use parenthesis and try

 if (charCode > 31 && (charCode < 48 || charCode > 57)){
        return false;
     }
    else{
  return true;
    }
brk
  • 48,835
  • 10
  • 56
  • 78
0

You can try below code snippet. it's same as Click

    <script type="text/javascript">
      function myKeyPress(e){
        var keynum;

        if(window.event) { // IE                    
          keynum = e.keyCode;
        } else if(e.which){ // Netscape/Firefox/Opera                   
          keynum = e.which;
        }
     if (keynum > 31 && (keynum < 48 || keynum > 57))
            return false;

return true;
      }
    </script>

    <form>
      <input type="text" onkeypress="return myKeyPress(event)" />
    </form>
Community
  • 1
  • 1
Niks
  • 175
  • 7