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