Home > Web Front-end > JS Tutorial > js generates verification code and judges it directly on the front end_javascript skills

js generates verification code and judges it directly on the front end_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:58:46
Original
1592 people have browsed it

JS generates the verification code and judges it directly on the front end

  <script type="text/javascript" src="img/jquery-1.5.1.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>
  <script type="text/javascript">
    $(document).ready(function () {

      $("#input1").blur(function () {
        var inputCode = document.getElementById("input1").value;
        if (inputCode.length <= 0) {
          alert("请输入验证码!");
        }
        else if (inputCode != code2) {
          alert("验证码输入错误!");
          createCode(); //刷新验证码
        }
        else {
          alert("^-^ OK");
        }
      });
    })
  </script>
Copy after login

HTML:

 <form action="#">
   <input type="text" id="input1" />
  <input type="text" onclick="createCode()" readonly="readonly" id="checkCode" class="unchanged" style="width: 80px;background: #660099"/><br />
  </form> 
Copy after login

css:

<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>
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template