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