<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>
说原理吧.1、js获取文本框的输入验证码。发送到后台的一个处理页面
2、处理页面接收js发送过来的值(参数)与seesion验证码对比。把对比结果输出(相当于返回结果了)
3、js接收后台处理页面返回来的结果(真或假),从而给用户提示验证码是否正确。
建议利用ajax比较方便。用Iframe实现也成。