This was an example from our prof and my HTML is rusty so I’m not sure exactly what is going on.
For the form input:
<input type="text" name="widgets" id="widgets" size="2" value="0" onchange="calc();" onkeypress="return isNumberInput(this, event);" />
For the Javascript:
function isNumberInput(field, event)
{
var key, keyChar;
if (window.event)
key = window.event.keyCode;
else if (event)
key = event.which;
else
return true;
// Check for special characters like backspace
if (key == null || key == 0 || key == 8 || key == 13 || key == 27)
return true;
// Check to see if it.s a number
keyChar = String.fromCharCode(key);
if (/\d/.test(keyChar))
{
window.status = "";
return true;
}
else
{
window.status = "Field accepts numbers only.";
return false;
}
Can someone explain what is going on? I’m not too familiar with window.event, event.which, wondow.event.keyCode, etc. I don’t really understand the logic. TIA!
1 Answer