JS数字相加验证表单
自己写的一个非常简单实用的“数字相加验证表单”JS,对自己还比较实用。
详细代码如下:
<body>
<form action="http://www.google.cn" method="post" onsubmit="return check()">
<div style="border:1px solid #6699CC; width:600px; height:180px;padding:10px;">
验证区域:<input id="mathAdd" value="ddd" /><span id="mathRandom" onclick="reLoad()" style="cursor:hand"></span>
<input type="submit" value="提交" />
</div>
</form>
<script language="javascript">
var num1=Math.round(Math.random()*10)//创建1~10之间的随机数,并取整
var num2=Math.round(Math.random()*10)//创建1~10之间的随机数,并取整
mathRandom.innerText=num1+"+"+num2+"=?";
function reLoad()//重新加载数字
{
num1=Math.round(Math.random()*10);
num2=Math.round(Math.random()*10);
mathRandom.innerText=num1+"+"+num2+"=?";
}
function check()//验证
{
var temp=document.getElementById("mathAdd").value;
if (temp!=(num1+num2))
{
alert("这么简单的相加都不会?你读过书没有哟!");
return false;
}
else
{
return true;
}
}
</script>
</body>
<form action="http://www.google.cn" method="post" onsubmit="return check()">
<div style="border:1px solid #6699CC; width:600px; height:180px;padding:10px;">
验证区域:<input id="mathAdd" value="ddd" /><span id="mathRandom" onclick="reLoad()" style="cursor:hand"></span>
<input type="submit" value="提交" />
</div>
</form>
<script language="javascript">
var num1=Math.round(Math.random()*10)//创建1~10之间的随机数,并取整
var num2=Math.round(Math.random()*10)//创建1~10之间的随机数,并取整
mathRandom.innerText=num1+"+"+num2+"=?";
function reLoad()//重新加载数字
{
num1=Math.round(Math.random()*10);
num2=Math.round(Math.random()*10);
mathRandom.innerText=num1+"+"+num2+"=?";
}
function check()//验证
{
var temp=document.getElementById("mathAdd").value;
if (temp!=(num1+num2))
{
alert("这么简单的相加都不会?你读过书没有哟!");
return false;
}
else
{
return true;
}
}
</script>
</body>