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.