Jquery: only numbers can be input in input, and the input method cannot be switched.

Source: Internet
Author: User

Code (Text in IE6, ie9, Mac, Google Crome):

$ (Function (){
$ ("# <% = Txtitem. clientid %>"). keydown (function (e ){

        // Do not use the keypress method here. Otherwise, CTRL + V and CTRL + V cannot be disabled. for specific reasons, identify the keypress and keydown. This is very important. Please refer to it for details.

If ($. browser. MSIE ){// Judge the browser

If (event. keycode> 47) & (event. keycode <58) | (event. keycode = 8 )){// Judge the key value

Return true;
} Else {
Return false;
}
} Else {
If (E. which> 47) & (E. which <58) | (E. which = 8) | (event. keycode = 17 )){
Return true;
} Else {
Return false;
}
}). Focus (function (){
This. style. imemode = 'Disabled ';// Disable the input method. Do not enter Chinese characters.

});
});

 

 

// Remarks:

// Imemode has four forms:
// Active indicates that the input method is Chinese.
// Inactive indicates that the input method is English
// Auto indicates that the input method is enabled (default)
// Disable indicates that the input method is disabled.

 

 

 

Colleagues' collection practices:

1. Regular Expressions limit float :( ^ [0-9] ([.] [0-9] {1, 2 })? $) | (^ 1 [0-9] ([.] [0-9] {1, 2 })? $) | (^ 2 [0-3] ([.] [0-9] {1, 2 })? $) | (^ 24 ([.] 0 {1, 2 })? $)
2. Restrict Input

Function keypress (objtr) {// only data characters 0-9 and decimal point can be entered
// Var objtr = element.doc ument. activeelement;
VaR txtval = objtr. value;
Var key = event. keycode;
If (Key <48 | key> 57) & Key! = 46 ){
Event. keycode = 0;
Alert ("only numbers allowed ");
}
Else {
If (Key = 46 ){
If (txtval. indexof (".")! =-1 | txtval. Length = 0)
Event. keycode = 0;
}
}
}

3. Do not switch the Input Method

Style: "ime-mode: Disabled"

 

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.