JS Verification Code generation (entry level)

Source: Internet
Author: User

Tag: Color round code cursor path generates height HTML func

Practiced hand project: JavaScript Verification Code

1 <!DOCTYPE HTML>2 <HTML>3 4     <Head>5         <MetaCharSet= "UTF-8">6         <title></title>7         <style>8 #mycanvas{9 cursor:Pointer;Ten             } One         </style> A     </Head> - <!--Place to optimize: random color of ① interference lines; ② can add a little bit of interference . - -     <Body> the         <CanvasID= "MyCanvas"width= ' All 'Height= ' + '> - your browser does not support canvas, please try a different browser ~ -         </Canvas> -     </Body> +  -     <Script> +         /*Generate 4-bit random numbers*/ A         functionrand () { at             varStr= "abcdefghijklmnopqrstuvwxyz0123456789"; -             vararr=Str.split (""); -             varValidate= ""; -             varRannum; -              for(varI= 0; I< 4; I++) { - Rannum=Math.floor (Math.random ()*  $); //random number between [0,35] in Validate+=Arr[rannum]; -             } to             returnValidate; +         } -  the         /*random x-coordinate value of the interference line*/ *         functionLineX () { $             varRanlinex=Math.floor (Math.random ()*  -);Panax Notoginseng             returnRanlinex; -         } the  +         /*random y-coordinate value of the interference line*/ A         functionLiney () { the             varRanliney=Math.floor (Math.random ()*  +); +             returnRanliney; -         } $  $         functionClickchange () { -             varMyCanvas=document.getElementById ('MyCanvas'); -             varCXT=Mycanvas.getcontext ('2d'); the Cxt.fillstyle= '#000'; - Cxt.fillrect (0, 0,  -,  +);Wuyi  the             /*generate 20 lines of interference*/ -              for(varJ= 0; J<  -; J++) { Wu Cxt.strokestyle= '#fff'; - Cxt.beginpath ();//if Beginpath is omitted, the number of lines of interference is accumulated for each click of the verification code. About Cxt.moveto (LineX (), Liney ()); $ Cxt.lineto (LineX (), Liney ()); - Cxt.linewidth= 0.5; - Cxt.closepath (); - Cxt.stroke (); A             } +  the Cxt.fillstyle= 'Yellow'; - Cxt.font= 'Bold 20px Arial'; $ Cxt.filltext (rand (), -,  -); //fills the random number text generated by rand () into the canvas the         } the  the Clickchange (); the  -         /*Click on the verification code to replace*/ in Mycanvas.onclick= function(e) { the E.preventdefault ();//prevent mouse clicks from occurring by default behavior the Clickchange (); About         }; the     </Script> the  the </HTML>

The results show:

JS Verification Code generation (entry level)

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.