JS implementation of serial number effect

Source: Internet
Author: User

Front-end friends may have encountered the need to enter a serial number on the page, or activation code (like the serial number in the PC genuine software), but the HTML does not provide us with similar components, we have to implement one ourselves:

The general idea is to have a hidden input in the form, instead of a set of input on the surface:

1 <formname=""Action=""Method= "POST">2 <P>3 <label>Please enter the Activation code:</label>4 <spanclass= "Act-code-group">5     <inputID= "Act-code-item-1"name= "Act-code-item-1"class= "Act-code-item"type= "text" /> - 6     <inputname= "Act-code-item-2"class= "Act-code-item"type= "text" /> - 7     <inputname= "Act-code-item-3"class= "Act-code-item"type= "text" /> - 8     <inputname= "Act-code-item-4"class= "Act-code-item"type= "text" />9 </span>Ten     <inputclass= "Act-code-hidden"type= "hidden"value="" /> One </P>

As for the design of the things left to everyone, their own play it!

And then we'll take care of the relevant logic:

1 functionInitactcodegroup (DOM) {2     varActcodegroup =DOM,3Actcodeitem = Actcodegroup.find ('. Act-code-item '));4     varCtrlpress =false;5     //actcodeitem.attr (' maxlength ', ' 5 ');6Actcodeitem.keyup (function(e) {7         varOldval = $ ( This). Val ();8         if(E.keycode = = 8){9             //BACKSPACETen             if(oldval.length <= 0){ One$( This). Prev (). focus (); A             } -}Else if(E.keycode = = &&ctrlpress) { -             //Ctrl + V the             varParsetxt = $ ( This). Val (), -Parsetxtlen =parsetxt.length; -  -Ctrlpress =false; +  -             varActcodes = []; +             if(Parsetxt.indexof ('-') >= 0){ AActcodes = Parsetxt.split ('-')); at}Else{ -                 if(Parsetxtlen >= 4) {Actcodes.push (parsetxt.substr (0, 4));} -                 if(Parsetxtlen >= 8) {Actcodes.push (Parsetxt.substr (4, 4));} -                 if(Parsetxtlen >= 12) {Actcodes.push (Parsetxt.substr (8, 4));} -                 if(Parsetxtlen >= 16) {Actcodes.push (Parsetxt.substr (12, 4));} -             } in  -             if(Actcodes.length > 1){ to                 varCurinput = $ ( This); +$.each (Actcodes,function(i, v) { -                     if(Curinput && v.length <= 4){ the Curinput.val (v); *Curinput =Curinput.next (); $}Else{Panax Notoginseng                         return false; -                     } the                 }); +             } A  the}Else if(E.keycode = = 17){ +SetTimeout (function(){ -Ctrlpress =false; $}, 1000); $}Else{ -             if(Oldval.length >= 4){ -$( This). Val (Oldval.substr (0,4)); the                 if($( This). Next (). length > 0){ -$( This). Next (). focus ();Wuyi                 } the             } -         } Wu}). KeyDown (function(e) { -         if(E.keycode = = 17){ AboutCtrlpress =true; $         } -     }); -}

This implements the serial number component of the 4*4, and the next call to the initialization function is good.

$ (document). Ready (function() {        Initactcodegroup ($ ('. Act-code-group ')
});

Open your browser and look at our serial number components!

JS implementation of serial number effect

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.