代码如下:
<html>
<head>
<title>随机产生指定位数的验证码</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="image/style.css" rel="stylesheet">
<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=document.getElementById("result");
result.innerHTML=" 产生的验证码:"+checkCode(form1.digit.value);
}
</script>
</head>
<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>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function checkCode(digit)
{
str="";
for(i=1;i<=digit;i++)
str+=parseInt(Math.random()*10);
return str;
}
function deal()
{
r1=document.getElementById("result");
r1.innerHTML=checkCode(document.f1.weishu.value);
}
</script>
</head>
<body onload="deal();">
<form id="f1" name="f1" method="post">
产生的验证码:<span id="result"></span><br>
请输入要产生的验证码的位数:<input type="text" id="weishu" value="4" /> <br />
<input type="button" value="生成" onclick="deal()"/>
<input type="button" value="刷新" onclick="window.location.reload();">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function checkCode()
{ var r1=document.getElementById("result");
var digit=document.f1.weishu.value;
str="";
for(i=1;i<=digit;i++)
str+=parseInt(Math.random()*10);
r1.innerHTML=str;
}
</script>
</head>
<body>
<form id="f1" name="f1" method="post">
产生的验证码:<span id="result"></span><br>
请输入要产生的验证码的位数:<input type="text" id="weishu" value="4" /> <br />
<input type="button" value="生成" onclick="checkCode()"/>
<input type="button" value="刷新" onclick="window.location.reload();">
</form>
</body>
</html>