Example code for JAVASCRIPT Verification Code (with effect diagram) _javascript tips

Source: Internet
Author: User

The effect is as follows:

Copy Code code as follows:

<title></title>
<style type= "Text/css" >
. Code
{
Background:url (code_bg.jpg);
font-family:arial;
Font-style:italic;
Color:blue;
font-size:30px;
border:0;
PADDING:2PX 3px;
letter-spacing:3px;
Font-weight:bolder;
Float:left;
Cursor:pointer;
width:150px;
height:60px;
line-height:60px;
Text-align:center;
Vertical-align:middle;

}
A
{
Text-decoration:none;
font-size:12px;
Color: #288bc4;
}
A:hover
{
Text-decoration:underline;
}
</style>
<script language= "javascript" type= "Text/javascript" >

var code;
function Createcode () {
Code = "";
var codelength = 6; The length of the Verification code
var checkcode = document.getElementById ("Checkcode");
var codechars = 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 ',
' 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 '); All the candidate components of the verification code, of course, can also be used in Chinese
for (var i = 0; i < codelength; i++)
{
var charNum = Math.floor (Math.random () * 52);
Code + + Codechars[charnum];
}
if (Checkcode)
{
Checkcode.classname = "code";
checkcode.innerhtml = code;
}
}
function Validatecode ()
{
var inputcode = document.getElementById ("Inputcode"). Value;
if (inputcode.length <= 0)
{
Alert ("Please enter the verification code!") ");
}
else if (inputcode.touppercase ()!= code.touppercase ())
{
Alert ("The authentication code input is wrong!") ");
Createcode ();
}
Else
{
Alert ("Verify code is correct!) ");
}
}
</script>

<body onload= "Createcode ()" >
<form id= "Form1" runat= "Server" onsubmit= "Validatecode ()" >
<div>
<table border= "0" cellspacing= "5" cellpadding= "5" >
<tr>
<td></td><td> <div class= "code" id= "Checkcode" onclick= "Createcode ()" ></div></td >
<td><a href= "#" onclick= "Createcode ()" > can't see a replacement </a></td>
</tr>
<tr>
<td> Verification Code: </td><td><input style= "Float:left" type= "text" id= "Inputcode"/></td><td > Please enter the verification code </td>
</tr>
<tr><td></td><td><input id= "Button1" onclick= "Validatecode ();" type= "button" value= "OK" /></td><td></td>
</tr>
</table>
</div>
</form>
</body>

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.