Verification code based on JavaScript, javascript Verification Code

Source: Internet
Author: User

Verification code based on JavaScript, javascript Verification Code

The example in this article shares the code for implementing the verification code in Javascript for your reference. The details are as follows:

1. A simple example
Create test.html

<! DOCTYPE html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> 

Create checkCode. js

Var code; // define the verification code window globally. onload = function createCode () {code = ""; var codeLength = 4; // The length of the Verification code var checkCode = document. getElementById ("code"); var random = new Array (, '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. random () * 36); // index for random number acquisition (0 ~ 35) code + = random [index]; // Add a random number based on the index to the code} checkCode. value = code; // assign the code value to the Verification code} // verify the verification code function validate () {var inputCode = document. getElementById ("input "). value. toUpperCase (); // obtain the entered verification code and convert it to uppercase if (inputCode. length <= 0) {// If the entered verification Code is 0 alert ("Empty Code! "); // Enter the verification code} else if (inputCode! = Code) {// If the entered verification Code is inconsistent with the generated verification code, alert ("Error Code"); // The Error code "createCode ()" is displayed (); // refresh the verification code document. getElementById ("input "). value = ""; // clear the text box} else {// when the input is correct, alert ("OK"); // pop up ^-^ }}

2. The code that does not move the position when you click:

<P class = "red"> <a href = "javascript:;" rel = "external nofollow" onclick = "createCode ()"> cannot see clearly? </A> </p>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.