随机生成验证码软件_JS生成随机验证码校验(前端)

点击上方“执行上下文”,选择“置顶公众号”

关键时刻,第一时间送达!

0465879a2a523d6023fa6fb5108bd0ee.gif 67f15c24b5a4f1f2def7fee7c9a76c6a.gif

HTML

"box">
"code" id="checkCode">{{nowCode}}
"###" @click="changeCode">看不清,换一张type="text" placeholder="请输入验证码" v-model="inputCode">"subBtn" @click="isLegalCode">验证

CSS

.box {
 width:300px;
 height:150px;
 border:1px solid;
 margin:30px auto;
 position:relative;
}
.code {
 width:120px;
 height:40px;
 background-color:#D6E3BC;
 border:2px solid;
 text-align:center;
 font-size:24px;
 line-height:40px;
 position:absolute;
 top:20px;
 left:20px;
 letter-spacing:3px;
}
a {
 position:absolute;
 left:160px;
 top:30px;
 color:green;
}
input {
 width:180px;
 height:20px;
 top:90px;
 left:20px;
 position:absolute;
}
button {
 width:50px;
 height:26px;
 position:absolute;
 top:90px;
 right:26px;
 background-color:green;
 color:#fff;
 border:1px solid gray;
}

JS

// 生成验证码
createCode () {
    let codeLength = 6; // 验证码长度
    let codeChars = 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',
        '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         let charIndex = Math.floor(Math.random() * 52); // 随机产生0-52之间的整数
        this.nowCode += codeChars[charIndex]; 
    }
},
// 切换验证码
changeCode() {
    this.createCode();
},
// 校验验证码
isLegalCode() {
    if (this.inputCode == this.nowCode) {
        alert("验证码正确!");
    } else {
        alert("验证码不正确,请重试!");
    }
}

前端公众号和交流群

b19c7d77cf396290673da1486afdd74f.gif

ac422f8ff566124e965d5072dcd068fe.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值