JavaScript generates verification code and implements validation _javascript tips

Source: Internet
Author: User

Nonsense not much said, directly to everyone to paste code, the specific code as follows:

 
 

Checkcode.js

 var code;//in Global definition Authenticode//generate CAPTCHA window.onload = function Createcode () {code = ""; 
var codelength = 4;//The length of the authentication code var checkcode = document.getElementById ("code"); var random = new Array (0,1,2,3,4,5,6,7,8,9, ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ' , ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '); random number for (var i = 0; i < codelength i++) {//cyclic operation var index = Math.floor (Math.ran 
Dom () *36); Index of random number (0~35) code + random[index];//add random number to code based on index checkcode.value = code;//Assign code value to verification code}//Checksum verification code function validate () {var inputcode = document.getElementById ("input"). Value.touppercase ();//Get the input verification code and convert it to uppercase if ( Inputcode.length <= 0) {//If the length of the authentication code entered is 0 alert ("Please enter the verification code!"). "); Then eject please enter the verification code} else if (Inputcode!= code) {//If the authentication code entered is inconsistent with the resulting Authenticode ("Authenticode input Error!"). @_@"); Then eject the authentication code input error Createcode ()//Refresh the Authentication code document.getElementById ("input"). Value = "";/Empty text box} else {//input correct alert ("^-^"); Popup ^}} 

The above is a small series to introduce the JavaScript to generate verification code and verify the function, 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!

Related Article

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.