I am currently dealing with a JavaScript routine that I did not create. This routine is triggered by the onkeydown
attribute of a text box in order to restrict certain keystrokes.
The first argument does not seem to have any significance. The second argument consists of a list of characters that are allowed.
function RestrictChars(evt, chars) {
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27))
// Control key
return true;
else if (((chars).indexOf(keychar) > -1))
return true;
else
return false;
}
This function appears to be functioning properly for alphanumeric characters. However, it seems to return false
when characters such as .
and /
are pressed, despite these characters being included in the chars
parameter. For instance, pressing the .
key sets key
to 190 and keychar
to the "3/4" character.
Can somebody clarify how this was intended to work and/or why it is not working correctly? My knowledge of JavaScript is limited, so I'm not able to discern its intended functionality.