layui 验证码功能

· 实现简易验证码功能

 style 样式部分

<style>
     #code {
		color:#1E9FFF;
		font-weight: bold;
		font-style:oblique;
		letter-spacing:5px;
		font-size:16px;
	 }
</style>

body 部分 

<body>
	<div style="width: 200px;display:flex;">
		<input type="text" name="captcha" placeholder="请输入验证码" maxlength="4" autocomplete="off"  id="ctl00_txtcode">
		<input type="button" id="code" onclick="createCode()" />
	</div>  
	<button type="button" class="layui-btn admin-button" id="login">登 录</button>
</body>

script 部分

<script>
	 //产生验证码
	 window.onload = function () {
		 createCode()
	 }
	 var code; //在全局定义验证码
	 function createCode() {
		 code = "";
		 var codeLength = 4; //验证码的长度
		 var checkCode = document.getElementById("code");
		 var random = new Array(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++) { //循环操作
			 var index = Math.floor(Math.random() * 36); //取得随机数的索引(0~35)
			 code += random[index]; //根据索引取得随机数加到code上
		 }
		 checkCode.value = code; //把code值赋给验证码
	 }
	 //校验验证码
	 function validate() {
		 var inputCode = $('#ctl00_txtcode').value.toUpperCase(); //获取输入框内验证码并转化为大写
		 if (inputCode.length <= 0) { //若输入的验证码长度为0
			 alert("请输入验证码!"); //则弹出请输入验证码
		 }
		 else if (inputCode != code) { //若输入的验证码与产生的验证码不一致时
			 alert("验证码输入错误!"); //则弹出验证码输入错误
			 createCode(); //刷新验证码
			 document.getElementById("ctl00_txtcode").value = "";//清空文本框
		 } else { //输入正确时
			 alert("正在登陆"); //弹出
		 }
	}
		 
	 //进行登录操作时 验证码判断
	 layui.use(['form','layer'], function () {
		 var form = layui.form,
			layer = layui.layer;
		 $("#login").click(function () {
			 var inputCode = $('#ctl00_txtcode').val().toUpperCase(); //获取输入框内验证码并转化为大写
			 if (inputCode == '') {
				 layer.msg('请输入验证码', { time: 1000 })
				 return
			 }
			 if (inputCode != code) {
				 layer.msg('验证码不正确', { time: 1000 })
				 return
			 }
		})
	})
</script>

此小案例引用了jQuery和Layui,以下是完整代码。

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>测试验证码</title>
	<style>
	 #code {
			color:#1E9FFF;
			font-weight: bold;
			font-style:oblique;
			letter-spacing:5px;
			font-size:16px;
			}
	</style>
</head>
<body>
	<div style="width: 200px;display:flex;">
		<input type="text" name="captcha" placeholder="请输入验证码" maxlength="4" autocomplete="off"  id="ctl00_txtcode">
		<input type="button" id="code" onclick="createCode()" />
	</div>  
	<button type="button" class="layui-btn admin-button" id="login">登 录</button>
</body>
<script src='layui-v2.5.5/layui.js'></script>
<script src='jquery-3.4.1/jquery-3.4.1.min.js'></script>
<script>
	 //产生验证码
	 window.onload = function () {
		 createCode()
	 }
	 var code; //在全局定义验证码
	 function createCode() {
		 code = "";
		 var codeLength = 4; //验证码的长度
		 var checkCode = document.getElementById("code");
		 var random = new Array(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++) { //循环操作
			 var index = Math.floor(Math.random() * 36); //取得随机数的索引(0~35)
			 code += random[index]; //根据索引取得随机数加到code上
		 }
		 checkCode.value = code; //把code值赋给验证码
	 }
	 //校验验证码
	 function validate() {
		 var inputCode = $('#ctl00_txtcode').value.toUpperCase(); //获取输入框内验证码并转化为大写
		 if (inputCode.length <= 0) { //若输入的验证码长度为0
			 alert("请输入验证码!"); //则弹出请输入验证码
		 }
		 else if (inputCode != code) { //若输入的验证码与产生的验证码不一致时
			 alert("验证码输入错误!"); //则弹出验证码输入错误
			 createCode(); //刷新验证码
			 document.getElementById("ctl00_txtcode").value = "";//清空文本框
		 } else { //输入正确时
			 alert("正在登陆"); //弹出
		 }
	}
		 
	 //进行登录操作时 验证码判断
	 layui.use(['form','layer'], function () {
		 var form = layui.form,
			layer = layui.layer;
		 $("#login").click(function () {
			 var inputCode = $('#ctl00_txtcode').val().toUpperCase(); //获取输入框内验证码并转化为大写
			 if (inputCode == '') {
				 layer.msg('请输入验证码', { time: 1000 })
				 return
			 }
			 if (inputCode != code) {
				 layer.msg('验证码不正确', { time: 1000 })
				 return
			 }
		})
	})
</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值