function createCode()
{ //创建验证码函数
code = ""
var codeLength =5//验证码的长度
var selectChar = 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')//所有候选组成验证码的字符,当然也可以用中文的
for(var i=0i<codeLengthi++)
{
var charIndex =Math.floor(Math.random()*36)
code +=selectChar[charIndex]
}// 设置验证码的显示样式,并显示
document.getElementById("discode").style.fontFamily="Fixedsys" //设置字体
document.getElementById("discode").style.letterSpacing="3px" //字体间距
document.getElementById("discode").style.color="#ff0000" //字体颜色
document.getElementById("discode").innerHTML=code // 显示
}</script>把上面一段JS代码放到<head></head>标签中在页面装载的时候,调用验证码创建函数 <body onload="createCode()
">在验证码输入框的后面放一个标签 <span id="discode"></span>
<html><head>
<script language="javascript" type="text/javascript">
var code
function createCode(){
code = new Array()
var codeLength = 4
var checkCode = document.getElementById("checkCode")
checkCode.value = ""
var selectChar = new Array(2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z')
for(var i=0i<codeLengthi++) {
var charIndex = Math.floor(Math.random()*32)
code +=selectChar[charIndex]
}
checkCode.value = code
}
function validate () {
var inputCode = document.getElementById("yzm").value.toUpperCase()
if(inputCode != code ){
alert("验证码错误!")
return false
}
else {
alert("验证码正确!")
return true
}
}
</script>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
</head>
<body onLoad="createCode()">
验证码:<input type="text" id="yzm" size="5" />
<input type="button" id="checkCode" onClick="createCode()" title="刷新验证码" style="width:50pxcolor:#F00border:0letter-spacing:1pxfont-family:Arial" />
<input type="button" id="Button1" onClick="validate()" value="确定" />
</body>
</html>