0

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,...

Andy E
  • 338,112
  • 86
  • 474
  • 445
Egy Mohammad Erdin
  • 3,402
  • 6
  • 33
  • 57

2 Answers2

1

@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

Community
  • 1
  • 1
Egy Mohammad Erdin
  • 3,402
  • 6
  • 33
  • 57
0

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)">
Eric Fortis
  • 16,372
  • 6
  • 41
  • 62