please i don’t really know what’s going on here, i know that if window.event(true) assign event.keyCode to key…what does the the next if block do? whats the meaning of event.keyCode == 8 and the others
function validateQty(event) {
var key = window.event ? event.keyCode : event.which;
if (event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
return true;
}
else if ( key < 48 || key > 57 ) {
return false;
}
else return true;
};
I’ll be applying it to a keydown event
if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39) //If the key that is pressed has keycode 8 or 46 or 37 or39…
{ return true;} //… then return true.
else if ( key < 48 || key > 57 ) //otherwise if the key pressed has keycode less than 48 or greater than 57…
{ return false;}//then return false
}else return true;};//otherwise return true
Another way to say this is:
return true if the key pressed is: 8, 37, 39, 46, 48-57.
return false if any other key.
I think there are better ways to write this and I don’t have the keycodes memorized so I don’t know which keys they’re referring to but that’s whats happening.
-J
so the keyCode is like a character that represents a keyboard key,also what does the return true represent
Right, each key on the keyboard has a keyCode. Capital letters are different from lowercase letters. For example, keyCode: 8 is “backspace”. You can google them to look them up.
return true returns a boolean value of true. It’s a simple true/false value. Usually you take that and do something else with it.
-J