Search code examples
javascripthtmlonkeydown

weird bug but on javascript onkeydown


i have this script working but it allows letters and disallows numbers along the top of the keyboard but for some reason it allows the number pad. how do i make it so it only allows lower case letters?

function isAlphaKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode;
     if ((charCode==231 || charCode==199) || (charCode==241 || charCode==209) ||(charCode==8 || charCode==32) || ( (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) ) ) {
        return true;
     }
     else {
         return false;
     }
} 

Solution

  • Modified code, allowing only 231, 199, 241, 209, 8, 32 and lower case characters

    var allowedNumber = [231, 199, 241, 209, 8, 32];
    function isAlphaKey(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode;
         if ( allowedNumber.indexOf(charCode) != -1 || ( charCode >= 97 && charCode <= 122) ) {
            return true;
         }
         else {
             return false;
         }
    }