Tag: event. keycode
<SCRIPT>
Function onlynum ()
{
If (! (Event. keycode = 46 )&&! (Event. keycode = 8 )&&! (Event. keycode = 37 )&&! (Event. keycode = 39 ))
{
If (! (Event. keycode> = 48 & event. keycode <= 57) | (event. keycode> = 96 & event. keycode <= 105 )))
{
Event. returnvalue = false;
}
}
}
</SCRIPT>
<Input onkeydown = "onlynum ()"/>
Set the event object when keydown occurs. The keycode is an attribute of the event object, which specifies the Unicode encoding code generated by the hitting key.
The above method is parsed:
Press Enter: 8 delete: 46 direction keys left: 37 direction keys right: 39
Top of the keyboard: 0-9: 48-57 keypad number key: 0-9: 96-105
The logic of the above method is: if it is not a digit → backspace Delete, it will judge whether the input is a number. If it is not a number, it will set the returnvalue attribute of the event object to false, cancels the default action of the event source element.