开发者

how to disable some key like @,!%" and compatible in ie and FF?

sorry if my english bad..

let's to the point

here my HTML:

<input type="text" name="hostname" onkeypress="return keyPressHostName(event)">

and this my JS:

function keyPressHostName(e) {
    var input = e.keyCode ? e.keyCode : e.charCode;
    if ((input >= 48 && input <= 57) // 0-9
    || (input >= 65 && input <= 90) //a-z
    || (input >= 97 && input <= 122) //A-Z
    || (input == 45) // dash (-)
    || (input == 46) // point(.)
    || (input == 37) // left key (<-)
    || (input == 39) // right key (->)
    || (input == 8) // backspace
    || (input == 9) // tab
    ) 
        return true;
    else 
        return false;
}
/*alert(input);
    if ((e.keyCode==45)||(e.keyCode==46)||(e.keyCode==37)||(e.keyCode==39)||(e.keyCode==8)||(e.keyCode==9)||(e.keyCode==39)) 
        return true;
    else if ((e.charCode>=48 && e.charCode<=57)||(e.charCode>=65 && e.charCode<=90)||(e.charCode>=97 && e.charCode<=122)||e.charCode==45||e.charCode==46) 
        return true开发者_开发技巧;
    else
        return false;
*/

but there are some error

  • the % (percent key) return true... and when i traced it. percent key is 37, same as left key
  • in IE, when user press left,right,backspace, or delete... the cursor move to end

anyone can suggest me a better way?

in my text field there only can receive 0-9,a-z,A-Z,dash,and point,...


@Eric Fortis: i've discuss to my teacher and he said keyCode or charCode in Opera,IE,FireFox etc is different....

so my script like this

<input type="text" name="hostname" onkeypress="return keyPressHostName(event)">

and the js like this:

function keyPressHostName(e)
{
    if (navigator.appName=="Opera")
    {
        var c= String.fromCharCode(e.keyCode);
        if ((e.keyCode==37)// left
            ||(e.keyCode==8)// <== backspace
            ||(e.keyCode==9)// tab
            ||(e.keyCode==39)// right
        )return true;
        else if (/\_/.test(c))return false;
        else if (/[\w\-\.]/.test(c))return true;
        else return false;
    }
    else if (navigator.appName =="Netscape")
    {
        var c= String.fromCharCode(e.charCode); 
        if (/\_/.test(c))return false;
        else if (/[\w\-\.]/.test(c))return true;
        else if (e.keyCode>=1)return true;
        else return false;
    }
    else if (/Microsoft/.test(navigator.appName)){
        var c= String.fromCharCode(e.keyCode);  
        if (/\_/.test(c))return false;
        else if (/[\w\-\.]/.test(c))return true;
        else return false;
    }
    else
    {
        var CharKey = (e.keyCode)?e.keyCode:e.charCode;
        var c= String.fromCharCode(CharKey);
        if (/\_/.test(c))return false;
        else if (/[\w\-\.]/.test(c))return true;
        else return false;
    }
}

i also found this JavaScript KeyCode vs CharCode


In order to get the special keys, onkeypress should be changed to onkeyup OR onkeydown

Edit:

<input type="text" name="hostname" onkeyup="return keyPressHostName(event)">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜