运算验证码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<style type="text/css">
		   .code
    {
      font-family: Arial;
      font-style: italic;
      color: Red;
      border: 0;
      padding: 2px 3px;
      letter-spacing: 3px;
      font-weight: bolder;
    }
    .unchanged
    {
      border: 0;
    }
	</style>
	<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<script language="javascript" type="text/javascript">
  var code; //在全局 定义验证码
  var code2; //在全局 定义验证码
  function createCode() {
    code = "";
    var checkCode = document.getElementById("checkCode");
    function RndNum(n) {
      var rnd = "";
      for (var i = 0; i < n; i++)
        rnd += Math.floor(Math.random() * 10);
      return rnd;
    }
 
    var num = RndNum(2);
    var num2 = RndNum(2);
 
    code = num + "+" + num2 + "=";
    code2 = parseInt(num) + parseInt(num2)
 
    if (checkCode) {
      checkCode.className = "code";
      checkCode.value = code;
    }
  }
</script>
	<body onload="createCode() ">
		<form action="#">
			 <input type="text" id="input1" />
			 <input type="text" onclick="createCode()" readonly="readonly" id="checkCode" class="unchanged" style="width: 80px;background-color: gainsboro;"/><br />
		     <input type="submit" value="验证"  id="validate"/>
		
		</form> 
	</body>
</html>

<script type="text/javascript">
  $(document).ready(function () {
 
    $("#validate").click(function () {
      var inputCode = document.getElementById("input1").value;
      if (inputCode.length <= 0) {
        alert("请输入验证码!");
      }
      else if (inputCode != code2) {
        alert("验证码输入错误!");
        createCode(); //刷新验证码
      }
      else {
        alert("验证码输入正确!");
      }
    });
  })
</script>

  

 

posted @ 2017-07-21 10:20  陈观爱  阅读(447)  评论(0编辑  收藏  举报