×

JS开发小应用:随机生成验证码

作者:Web前端之家2021.09.06来源:Web前端之家浏览:417评论:0
关键词:js

JS开发小应用:随机生成验证码。写一个简单的DEMO,供大家参考下,看下代码吧:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title>JS开发小应用:随机生成验证码 | Web前端之家www.jiangweishan.com</title>
  <style>   
  </style>
 </head>
 <body>
  <div>
  <h1>111111</h1><a href="#" rel="external nofollow" >看不清换一张</a>
  </div>
  验证码:<input type="text" value="" />
  <button>确定</button>
 </body>
 <script>
  var arr = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
       "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
       0,1,2,3,4,5,6,7,8,9];
        var h1=document.querySelector("h1");
        var btn=document.querySelector("button");
        var change=document.querySelector("a");     
        var input=document.querySelector("input");         
         function fn(){
        var arr1=Math.floor(Math.random()*62);
        var arr2=Math.floor(Math.random()*62);
        var arr3=Math.floor(Math.random()*62);
        var arr4=Math.floor(Math.random()*62);
        var arr5=Math.floor(Math.random()*62);
        var arr6=Math.floor(Math.random()*62);
        var yz=""+arr[arr1]+arr[arr2]+arr[arr3]+arr[arr4]+arr[arr5]+arr[arr6];
            h1.innerHTML=yz;    
            btn.onclick=function(){
              alert(input.value);
             if(input.value==yz){
            alert("正确");
            }else{
             alert("错误");
            }
           
            }
         }
        fn();
    change.addEventListener("click",fn);
 </script>
</html>


温馨提示:本文作者系Web前端之家 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/js20210906a2.html

网友评论文明上网理性发言 已有0人参与

发表评论:

最新留言

  • Web前端之家

    GoogleAdSense可以的,可以加群主,帮你搞定这事情~~...

  • s121

    GoogleAdSense好弄吗,想弄一个账号。...

  • s121

    不错,正好用到,收藏了!...

  • 访客

    什么机翻...

  • Web前端之家

    只是更轻便,还是有些差距的呢,小项目可以玩玩。...

  • ja124

    这个跟vue和react比,好些吗?...

  • Web前端之家

    OK,已经更新,谢谢!...

  • 韩涛博客

    韩涛博客的域名更换为www.hantaosec.com啦,请更新一下域名哈,谢谢。我还会继续链着咱们...

首页|JavaScript|HTML|HTML4|HTML5|CSS3|开发工具|性能优化|移动开发|前端教程|性能优化|开发工具|酷站欣赏|UI设计|前端教程

Copyright © 2021 Web前端之家(www.jiangweishan.com) 版权所有 All Rights Reserved.
粤ICP备12067512号-1

Copyright Your WebSite.Some Rights Reserved.

Powered By Z-BlogPHP 1.6.8 Valyria