Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
var specialKeys = new Array();
        specialKeys.push(8); //Backspace
        function IsNumericss(e) {
            var keyCode = e.which ? e.which : e.keyCode
            var ret = ((keyCode >= 49 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
            return ret;
        }


This allows numbers 1-9 only but I need to allow the period also or keycodes 110 & 190.
How would I go about doing this?
I tried something like this but to no avail:
var ret = ((keycode >= 49 && keycode <= 57 && keycode == 110 && keycode == 190)
Posted
Comments
Sergey Alexandrovich Kryukov 29-Dec-14 12:06pm    
All you had to to was to think just a bit; you was quite close. :-)
—SA

1 solution

This is well explained here: http://www.javascripter.net/faq/keyboardinputfiltering.htm[^].

You need to add an additional check.
JavaScript
ret = (49 <= keyCode && keyCode <= 57) ||
      (110 <= keyCode && keyCode <= 190) ||
      (8 == keyCode);
Note that I also removed redundant outer brackets and excluded the use of the array, which you used just for the check for backspace (8), which makes little sense. Keep it simple.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900