text box can only enter digital page code

Source: Internet
Author: User

The

  method One or four event combination enables a text box to be entered only with a number
<input type= "text"
onkeypress = ' return/^d$/.test ( String.fromCharCode (Event.keycode)) '
oninput= ' this.value = This.value.replace (/d+/g, "") '
onpropertychange= ' If (!/d+/.test (This.value)) {return;}; This.value=this.value.replace (/d+/g, "") '
onblur = ' This.value = This.value.replace (/d+/g, "") '/>
Methods two, jquery implementation text box can only enter a number
control text box can only enter numbers is a very common requirement, such as phone number input, number of input, etc., this time we need to control the text box can only enter numbers. After using JS control in the state of the English input method to hit the keyboard is not the number of keys to lose, however, when you go to the Chinese input method is entered in Chinese, and then by the space or enter, the Chinese or English letters to enter, this time we need to focus on the text box when the input method is closed, is to not respond to input method switching. The following statement can implement this functionality.
The following statement is written in jquery, and a text box that controls class Checknum can only enter numbers and decimal points.
JS code   Collection Code
//Monitor keyboard, only allow input numbers and decimal  
            $ (". Checknum"). KeyPress ( function (event) { 
                var keycode = Event.which;  
                if (keycode = 46 | | (KeyCode >= 48 && keycode <=57)  
                    return true; &NBSP
                Else  
          &NBS P         return false; &NBSP
           }). focus (function () { 
          & nbsp     this.style.imemode= ' disabled ';  
           }); &NBSP
IMEMode has four forms, respectively:
active on behalf of input method for Chinese
inactive on behalf of input method for English
Auto representative open Input Method (default)
Disable rep close IME

Related Article

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.