JQ Limit text box numeric decimal letter

Source: Internet
Author: User

Recently in the project to control the file box input, Google a bit, find some methods. and modified. The following code is validated and can be used normally.

// ----------------------------------------------------------------------//<summary>//The text box can only enter a number (excluding decimals) and mask the IME and paste//</summary>// ---------------------------------------------------------------------- $.fn.integer =function () {     This. Bind ("KeyPress",function(e) {varCode = (E.keycode E.keycode:e.which);//compatible with Firefox IE        if(!$.browser.msie && (E.keycode = = 0x8)) {//You can't use the backspace bar under Firefox            return; }        returnCode >= && Code <= 57;    });  This. bind ("Paste",function () {        return false;    });  This. Bind ("KeyUp",function () {        if(/(^0+)/.test ( This. Value)) {             This. Value = This. Value.replace (/^0*/, ");    }    });  This. Bind ("Focus",function () {         This. Style.imemode = ' disabled ';    });  This. Bind ("Blur",function () {        varReg =/^\d{1,}$/; if( This. Value.slice (-1) = = "") {             This. Value = "0"; }        if(Reg.test ( This. value) = = =false) {             This. Value = "0"; }    });};// ----------------------------------------------------------------------//<summary>//The text box can only enter numbers (including decimals) and mask input methods and paste//</summary>// ----------------------------------------------------------------------$.fn.number =function () {     This. Bind ("KeyPress",function(e) {varCode = (E.keycode E.keycode:e.which);//compatible with Firefox IE        if(!$.browser.msie && (E.keycode = = 0x8)) {//You can't use the backspace bar under Firefox            return; }        if( This. Value.indexof (".") = =-1) {            return(Code >= && Code <= 57) | | (Code = = 46); } Else {            returnCode >= && Code <= 57        }    });  This. bind ("Paste",function () {        return false;    });  This. Bind ("KeyUp",function () {        if( This. Value.slice (0, 1) = = ".") {             This. Value = "";    }    });  This. Bind ("Focus",function () {         This. Style.imemode = ' disabled ';    });  This. Bind ("Blur",function () {        varReg =/^-? [^\d]+\.? [^\d] {1,4}$/; if( This. Value.slice (-1) = = ".") {             This. Value = This. Value.slice (0, This. value.length-1); }        if( This. Value.slice (-1) = = "") {             This. Value = "0"; }        if(Reg.test ( This. value) = = =false) {             This. Value = "0"; }    });};// ----------------------------------------------------------------------//<summary>//limit input Letters only//</summary>// ----------------------------------------------------------------------$.fn.onlyalpha =function () {     This. Bind ("KeyPress",function(event) {varEVENTOBJ = Event | |e; varkeycode = Eventobj.keycode | |Eventobj.which; if((keycode >= && keycode <= 90) | | (KeyCode >= && keycode <= 122))            return true; Else            return false;    });  This. bind ("Paste",function () {        varClipboard = Window.clipboardData.getData ("Text"); if(/^[a-za-z]+$/. Test (clipboard))return true; Else            return false;    });  This. Bind ("Focus",function () {         This. Style.imemode = ' disabled '; });};// ----------------------------------------------------------------------//<summary>//limit input numbers and letters only//</summary>// ----------------------------------------------------------------------$.fn.onlynumalpha =function () {     This. Bind ("KeyPress",function(event) {varEVENTOBJ = Event | |e; varkeycode = Eventobj.keycode | |Eventobj.which; if((keycode >= && keycode <= 57) | | (KeyCode >= && keycode <= 90) | | (KeyCode >= && keycode <= 122))            return true; Else            return false;    });  This. bind ("Paste",function () {        varClipboard = Window.clipboardData.getData ("Text"); if(/^ (\d| [A-za-z]) +$/. Test (clipboard))return true; Else            return false;    });  This. Bind ("Focus",function () {         This. Style.imemode = ' disabled '; });};

Use the method to:

$ ("#Id"). Integer (), $ ("#Id"). Number (); $ ("#Id"). Onlyalpha (); $ ("#Id"). Onlynumalpha ();

JQ Limit text box numeric decimal letter

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.