Event. keycode controls that only numbers can be input in input.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.