2013-03-15 60 views
3

我有这个脚本工作,但它允许字母和不允许沿键盘顶部的数字,但由于某种原因,它允许数字键盘。我如何制作它,所以它只允许小写字母?奇怪的错误,但在JavaScript onkeydown

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; 
    } 
} 
+0

为什么不使用switch语句因为你的代码看起来完全混乱! – spaceman12 2013-03-15 20:59:08

+0

@ spaceman12他如何使用开关检测97至122之间的数字? – Anoop 2013-03-15 21:43:07

回答

2

修改后的代码,只允许231,199,241,209,图8,32和小写字符

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; 
    } 
} 
0

检查ASCII数字使用控制台日志(键事件),看看这些数字在正确的安培数是,并删除他们在你的条件声明