//填写验证码input
CheckCode:<input type="text" id="txtCheckCode" name="txtCheckCode" class="input" style="width:70px;">
//验证码显示区
<div id="imgCheckCode" style="width:44px; height:16px; position:absolute; right:0px; top:1px;">
<img src="images/number/8.gif"><img src="images/number/8.gif"><img src="images/number/8.gif"><img src="images/number/8.gif">
</div>
javascript代码:
var checkcode;
function random(startnum,endnum) //返回javascript生成的随机数
{
var upnum=Math.abs(endnum-startnum);
var ran=parseInt(Math.random()*10000000000000000);
ran=(ran+1)%upnum;
return ran+startnum;
}
function generate_checkcode() //绑定javascript生成的随机数
{
this.checkcode="";
var imgstr="";
for(var i=0;i<4;i++)
{
randnum=this.random(0,10);
this.checkcode += randnum;
imgstr+="<img src='images/number/"+randnum+".gif' width=11 height=16>";
}
this.imgcheckcode.innerHTML=imgstr;
}
function checkform() //在如登陆注册处对输入的验证码进行验证
{
if(this.txtcheckcode.value!=this.checkcode) { this.txtcheckcode.focus(); alert("Checkcode error!"); return false; }
}
javascript生成随机验证码中用到的图片素材:










javascript生成随机验证码详细代码讲解完毕,欢迎大家一起来研究和讨论25亿企业网站管理系统。
作者: 网站设计@ 企业网站管理系统
原载: 25亿企业网站管理系统
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。