如图所示:
随机产生指定位数的验证码的代码如下:
<script language="javascript">
function checkCode(digit){
//自动生成验证码
var result="";
for(i=0;i<parseInt(digit);i++){
result=result+(parseInt(Math.random()*10)).toString();
}
return result;
}
</script>
<script language="javascript">
function deal(){
result.innerHTML=" 产生的验证码:"+checkCode(form1.digit.value);
}
</script>
<body>
<form name="form1" method="post" action="">
<table width="271" height="178" border="0" cellpadding="0" cellspacing="0" background="image/result.gif">
<tr>
<td valign="top"><table width="100%" height="150" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="15%" height="55"> </td>
<td width="69%" valign="bottom"><div id="result"> 产生的验证码:</div></td>
<td width="16%"> </td>
</tr>
<tr>
<td height="68"> </td>
<td align="center">
<br>
请输入要产生的验证码的位数:<br>
<br>
<input name="digit" type="text" class="wenbenkuang" id="digit" value="4">
(1-15)</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="Submit" type="button" class="go-wenbenkuang" value="生成" onClick="deal()">
<input name="Submit2" type="button" class="go-wenbenkuang" value="刷新" onClick="window.location.reload();"></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>