原生js随机验证码

原生js随机验证码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>12 随机验证码</title>
	<style type="text/css">
		#code{
			width: 100px;
			height: 100px;
			background-color: #ddd;
			padding: 10px;
			line-height: 100px;
			text-align: center;
			font-size: 20px;
			color: red;
			font-weight: bold;
		}
		input{
			outline: none;

		}
	</style>
</head>
<body>

	<div id="code"></div>
	<input type="text" name="" id="newCode">
	<input type="button" name="" id="validate" value="验证">

	<script type="text/javascript">

		window.onload = function(){
			// 保存全局,与新输入的验证码进行校验
			var  code;
			// 1.获取对应的标签
			var codeDiv = document.getElementById('code');
			var newCodeInput = document.getElementById('newCode');
			var validate = document.getElementById('validate');
			// 加载页面获取对应的验证码
			creatCode()

			// 1.获取min到max之间的整数 (1~100)
			function random(max,min){
				return Math.floor(Math.random() * (max-min) + min);
			}
			function creatCode(){
				// 设置默认的空的字符串  
				 code = '';
				// 设置长度 
				var codeLength = 4;
				var randomCode = [0,1,2,3,4,5,6,7,8,9,'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'];
				for(var i  = 0; i < codeLength; i++){
					// 设置随机范围 0~36
					var index = random(0,36);
					code +=  randomCode[index];
				}
				codeDiv.innerHTML = code;
			}

			// 验证按钮校验
			validate.onclick = function(){
				// 获取用户新输入的验证码
				var  newCode = newCodeInput.value.toUpperCase();
				if(newCode === code){
					// 验证成功 跳转对应的网址
					window.location.href = 'https://www.apeland.cn';
				}else{
					// 验证失败
					alert('验证码不正确,请重新输入');
					// 输入框置空
					newCodeInput.value = ' ';
					// 重新获取验证码
					creatCode();
				}
			}
		}
	</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用原生 JavaScript 实现验证码。以下是一个简单的例子: HTML: ```html <label for="captcha">验证码:</label> <input type="text" id="captcha"> <canvas id="captchaCanvas"></canvas> <button id="refreshCaptcha">刷新验证码</button> ``` JavaScript: ```javascript var canvas = document.getElementById("captchaCanvas"); var ctx = canvas.getContext("2d"); // 生成随机验证码 function generateCaptcha() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var captchaLength = 6; var captcha = ""; for (var i = 0; i < captchaLength; i++) { captcha += chars.charAt(Math.floor(Math.random() * chars.length)); } return captcha; } // 绘制验证码 function drawCaptcha() { var captcha = generateCaptcha(); canvas.width = 100; canvas.height = 40; ctx.font = "bold 20px sans-serif"; ctx.fillStyle = "#000"; ctx.fillText(captcha, 25, 25); // 添加干扰线 for (var i = 0; i < 5; i++) { ctx.beginPath(); ctx.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); ctx.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); ctx.strokeStyle = "#000"; ctx.stroke(); } return captcha; } // 刷新验证码 document.getElementById("refreshCaptcha").addEventListener("click", function() { var captchaInput = document.getElementById("captcha"); captchaInput.value = ""; var captcha = drawCaptcha(); }); // 验证输入的验证码是否正确 document.getElementById("captcha").addEventListener("input", function() { var captchaInput = document.getElementById("captcha"); if (captchaInput.value.length == 6 && captchaInput.value.toUpperCase() == captcha) { alert("验证码正确!"); } }); ``` 这个例子首先定义了一个 `generateCaptcha` 函数来生成随机验证码,然后定义了一个 `drawCaptcha` 函数来绘制验证码,并在验证码中添加了一些干扰线以增加难度。最后使用 `addEventListener` 为刷新按钮和验证码输入框添加事件监听器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值