JS经典案例之随机生成验证码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style>			
		</style>
	</head>
	<body>
		<div>
		<h1>111111</h1><a href="#">看不清换一张</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>

1、Math.floor() 方法

Math.floor(x)

floor() 方法返回小于等于x的最大整数。

如果传递的参数是一个整数,该值不变。

2、Math.random()方法

random() 方法可返回介于 0(包含) ~ 1(不包含) 之间的一个随机数。


使用以上两种方法可以实现取随机数的功能

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值