jquery control text box can only enter numbers and letters and how to use _jquery

Source: Internet
Author: User

In the company's development of WinForm projects, found that the company's self-developed TextBox control is very powerful, you can achieve "only number", "Only the letter" and "only enter numbers and letters," the three kinds of input restrictions, so you can accurately control the user input range, so that " Users will never be able to enter content other than the limits of content ", that is," users do not have a chance to make mistakes, "this way of restricting control input gives me a lot of inspiration, if the Web project can do this precise control, so you can avoid some illegal input caused by the system error, Since the WinForm can implement such a control, then the Web project should also have a way to implement such a control or to achieve similar results, through their own research and find information, and finally achieved a similar effect, for "only enter the number", "Only the letter" and " Can only enter the number and letter "Three kinds of input restrictions, I encapsulated into Onlynum (), Onlyalpha () and Onlynumalpha () 3 jquery extension method, convenient reuse, because some of the JS code involved in the" Disable input method, get the contents of the Clipboard ", and" Disable Input method, get the contents of the Clipboard "can only be effective in IE browser, for other browsers are not valid, so these three methods are only suitable for use in IE browser only valid, three methods of code as follows

One, limit can only enter the number

----------------------------------------------------------------------
 //<summary>//
 limit only digits can be entered
 //</summary>
 //----------------------------------------------------------------------
 $. Fn.onlynum = function () {
   $ (this). KeyPress (function (event) {
     var eventObj = Event | | e;
     var keycode = Eventobj.keycode | | Eventobj.which;
     if ((keycode >= && keycode <=)) return
       true;
     else return
       false;
   }). Focus (function () {
   //disable IME
     this.style.imeMode = ' disabled ';
   }). Bind ("Paste", function () {
   //Get the contents of the Clipboard
     var clipboard = window.clipboardData.getData ("Text");
     if (/^\d+$/.test (clipboard)) return
       true;
     else return
       false;
   });
 

Second, the limit can only enter letters

----------------------------------------------------------------------
 //<summary>//
 limit only letters can be entered
 //</summary>
 //----------------------------------------------------------------------
 $. Fn.onlyalpha = function () {
   $ (this). KeyPress (function (event) {
     var eventObj = Event | | e;
     var keycode = Eventobj.keycode | | Eventobj.which;
     if ((keycode >= && keycode <=) | | (KeyCode >= && keycode <=))
       return true;
     else return
       false;
   }). Focus (function () {
     this.style.imeMode = ' disabled ';
   }). Bind ("Paste", function () {
     var clipboard = window.clipboardData.getData ("Text");
     if (/^[a-za-z]+$/.test (clipboard)) return
       true;
     else return
       false;
   });
 

Third, the limit can only enter numbers and letters

 ----------------------------------------------------------------------
 //<summary>/
 / Limit can only enter numbers and letters
 //</summary>/
 /--------------------------------------------------------------------- -
 $.fn.onlynumalpha = function () {
   $ (this). KeyPress (function (event) {
     var eventObj = Event | | e;
     var keycode = Eventobj.keycode | | Eventobj.which;
     if ((keycode >= && keycode <=) | | (KeyCode >= && keycode <=) | | (KeyCode >= && keycode <=))
       return true;
     else return
       false;
   }). Focus (function () {
     this.style.imeMode = ' disabled ';
   }). Bind ("Paste", function () {
     var clipboard = window.clipboardData.getData ("Text");
     if (/^ (\d|[ A-za-z]) +$/.test (clipboard)) return
       true;
     else return
       false;
   });
 

How to use: first after the screen loading complete the following JS script

 $ (function () {
   //Limit controls that use the Onlynum class style can enter only the number
   $ (". Onlynum"). Onlynum ();
   Controls that use the Onlyalpha class style can only enter the letter
   $ (". Onlyalpha"). Onlyalpha ();
   Controls that use the Onlynumalpha class style can only enter numbers and letters
   $ (". Onlynumalpha"). Onlynumalpha ();
  

Set the class style for controls that require input control

 <ul>
     <li> can only enter the number: <input type= "text" class= "Onlynum"/></li>
     <li> can only enter the letter:< Input type= "text" class= "Onlyalpha"/></li>
     <li> can only enter numbers and letters: <input type= "text" class= " Onlynumalpha "/></li>
 

This screen usually set the class= "Onlynum" control can only enter numbers, set the class= "Onlyalpha" control can only enter letters, set the class= " Onlynumalpha controls can only enter numbers and letters, which in this way limits the user's input and avoids some illegal input from the user.

The above is small set to introduce the jquery control text box can only enter numbers and letters and the use of methods, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.