四位验证码——精选推荐

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

四位验证码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>四个随机数</title>
<style>
#dd{
display: inline-block;
width: 100px;
height: 20px;
background-color: aqua;
position: relative;
top:5px;
}
</style>
</head>
<body>
<form action="#">
账号:<input type = "text"><br><br>
密码:<input type = "password"><br><br>
验证码:<input type = "text" id ="YZM">
<div id = "dd"></div>
<button onClick="yzm()">看不清</button><br>
<input type = "button" value = "登录" onClick="isTrue()"><br><!--点击登录按钮,跳转登录判断⽅法-->
<span id = "info" style = "color:red"></span><!--错误显⽰,通过id显⽰错误以及正确信息-->
</form>
</body>
</html>
<script type = "text/javascript">
window.onload = yzm;
var str = "";
function yzm(){
str = "";
var arr = ["1","2","3","4","a","s","d"];//验证码表
for(var i = 0;i< 4;i++){//循环四次,因为是4位验证码
var xiabiao = parseInt(Math.random()*arr.length);//获取验证码字符的数组长度,作为随机数⽣成的数的范围str = str + arr[xiabiao];//将得到的字符串长度作为数组的下标
}
document.getElementById("dd").innerHTML = str;//将获得的字符串给⾮标单元素赋值
console.log(str);
}
function isTrue(){
var userStr = document.getElementById("YZM").value;//获得⽤户⼿动输⼊的验证码
if(userStr == str){
document.getElementById("info").innerHTML = "验证码正确";
}else{
document.getElementById("info").innerHTML = "验证码错误";
}
}
</script>。

相关文档
最新文档