JS生成4位验证码

PHP

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        ?>     
    <input type="text" id="veri"><section id="s1"></section><section id="s2"></section>
        <button onclick="check()">提交</button>
        <script src="js.js"></script>
    </body>
</html>

JS

/* 设置id*/
function $(id){
    return document.getElementById(id);
}  
/*生成验证码*/
var v="";
window.onload=f1;
        function f1(){
    var s=new Array(0,1,2,3,4,5,6,7,8,9);  
   for(var i=0;i<4;i++){
           var r=Math.floor(10*Math.random());
       var a=s[r];
               v+=a;      
   }   
 $('s1').innerHTML=v;   
 }
/*检验验证码*/
 ; 
function check(){     
 var veri=$('veri').value;  

   if(veri!==v){ 
      window.location.reload();
   }
   else{
   $('s2').innerHTML="Right";      
   }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42467319/article/details/87641175