Use jquery to control text boxes to enter only numbers and letters

Source: Internet
Author: User

Only to find a way to succeed, not to find excuses for failure!

Use jquery to control text boxes to enter only numbers and letters

When the company developed the WinForm project, found that the company's own research and development of the TextBox control is very powerful, can achieve "only enter the number", "only enter letters" and "only enter numbers and letters" Three input restrictions, so that you can precisely control the user input content range, so that " Users will never be able to enter content beyond the scope of the restricted content ", that is," users even if they want to make a mistake, there is no chance, "the way to restrict the control input gives me a lot of inspiration, if the Web project can also be done with such precise control, you can avoid some illegal input caused by system error, Since WinForm inside can implement such a control, then the Web project should also have a way to implement similar to the control or can do similar effects, after their own research and find data, finally achieve a similar effect, for "only enter the number," "Only enter the letter" and " Can only enter numbers and letters "Three kinds of input restrictions, I encapsulated into Onlynum (), Onlyalpha () and Onlynumalpha () 3 jquery extension methods, easy to reuse, because some JS code inside the" Disable Input method, get clipboard content ", and" Disable Input method, get clipboard content "only in IE browser is valid, for other browsers is not valid, so these three methods only suitable for use in IE browser only valid, three methods of code as follows

First, limit the number can only be entered
1//----------------------------------------------------------------------2//<summary> 3//Limit only enter the number 4//</ Summary> 5//----------------------------------------------------------------------6 $.fn.onlynum = function () {7     $ (this). KeyPress (event) {8         var eventObj = Event | | E; 9         var keycode = Eventobj.keycode | | eventObj . which;10         if ((keycode >= && keycode <=) return true;12 else13             return false;14< c7/>}). focus (function () {     //disable IME         this.style.imeMode = ' disabled '; n     }). bind ("Paste", function () {     //Get the contents of the Clipboard         var clipboard = window.clipboardData.getData ("Text");         if (/^\d+$/.test (clipboard) )             return true;22         else23             return false;24     }); 25};
Second, limit the input of letters only
1//----------------------------------------------------------------------2//<summary> 3//Limit only enter the letter 4//</ Summary> 5//----------------------------------------------------------------------6 $.fn.onlyalpha = function () {7     $ (this). KeyPress (function (event) {8         var eventObj = Event | | E; 9         var keycode = Eventobj.keycode | | event Obj.which;10         if ((keycode >= && keycode <= 90) | | (KeyCode >= && keycode <= 122))             return true;12         else13             return false;14     }). focus (function () {         This.style.imeMode = ' disabled     bind ("Paste", function () {$         var clipboard = window.clipboardData.getData ("Text"), and         if (/^[ A-za-z]+$/.test (clipboard))             return true;20         else21             return false;22     }); 23};
Third, limit the number and letters can only be entered
1//----------------------------------------------------------------------2//<summary> 3//Limit only enter numbers and letters 4//&L T;/summary> 5//----------------------------------------------------------------------6 $.fn.onlynumalpha = function () {7     $ (this). KeyPress (function (event) {8         var eventObj = Event | | E; 9         var keycode = EVENTOBJ.KEYC Ode | | Eventobj.which;10         if ((keycode >= && keycode <= 57) | | (KeyCode >= && keycode <= 90) | | (KeyCode >= && keycode <= 122))             return true;12         else13             return false;14     }). focus (function () {         This.style.imeMode = ' disabled     ). Bind ("Paste", function () {~         var clipboard = window.clipboardData.getData ("Text"),         if (/^ (\d | [A-za-z]) +$/.test (clipboard))             return true;20         else21             return false;22     }); 23};

How to use: First write the following JS script after the screen loading is complete

1 $ (function () {2     //Limit controls that use the Onlynum class style can only enter the number 3     $ (". Onlynum"). Onlynum (); 4     // Restrict controls that use the Onlyalpha class style to enter only the letter 5     $ (". Onlyalpha"). Onlyalpha (); 6     //Restrict controls that use Onlynumalpha class styles to enter only numbers and letters 7     $ (". Onlynumalpha "). Onlynumalpha (); 8    });

Set the class style for controls that require input control

1  <ul>2         <li> can only enter numbers: <input type= "text" class= "Onlynum"/></li>3         <li> You can only enter letters: <input type= "text" class= "Onlyalpha"/></li>4         <li> can only enter numbers and letters: <input type= "Text" class= "Onlynumalpha"/></li>5  </ul>

In this case, the class= "Onlynum" control on the screen can only enter a number, set the class= "Onlyalpha" control can only enter letters, set the class= "Onlynumalpha" control can only enter numbers and letters, In this way, the user's input range can be limited and the user is prevented from making some illegal input.

Use jquery to control text boxes to enter only numbers and letters

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.