老师检查作业
来源:2-25 项目作业
听的说
2021-12-30 19:32:12
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<body>
<style>
span {
display: inline-block;
width: 100px;
height: 15px;
background-color: #eee;
}
</style>
<form action="">
<div>
<label for="name">用户名</label>
<input type="text" id="name" placeholder="6-30字母数字,下划线,字母开头">
</div>
<div>
<label for="password">密码</label>
<input type="text" id="password">
<span></span>
<span></span>
<span></span>
</div>
<div>
<label for="agen">再一次输入</label>
<input type="text" id="agen">
</div>
<button type="button" id="btn">注册</button>
</form>
<script>
var btn = document.getElementById("btn");
var username = document.getElementById("name");
var password = document.getElementById("password");
var agen = document.getElementById("agen");
var span = document.getElementsByTagName("span");
username.onblur = function() {
if (/^[a-zA-Z](\w{5,29})$/.test(username.value)) {
alert("用户名输入正确");
} else {
alert("输入有误")
username.value = "";
}
};
password.onblur = function() {
if (/^[a-zA-Z0-9]{6,20}$/.test(password.value)) {
if (/^[A-Z]+$/g.test(password.value) || /^[a-z]+$/g.test(password.value) || /^\d+$/g.test(password.value)) {
span[0].style.backgroundColor = "red";
span[1].style.backgroundColor = "#eee";
span[2].style.backgroundColor = "#eee";
} else if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/.test(password.value)) {
span[0].style.backgroundColor = "red";
span[1].style.backgroundColor = "yellow";
span[2].style.backgroundColor = "green";
} else if (/[\dA-Z]$/g.test(password.value) || /[\da-z]$/g.test(password.value) || /[a-zA-Z]$/g.test(password.value)) {
span[0].style.backgroundColor = "red";
span[1].style.backgroundColor = "yellow";
span[2].style.backgroundColor = "#eee";
}
alert("用户名输入正确");
} else {
alert("输入有误");
span[0].style.backgroundColor = "#eee";
span[1].style.backgroundColor = "#eee";
span[2].style.backgroundColor = "#eee";
password.value = "";
}
};
agen.onblur = function() {
if (password.value == agen.value) {
alert("密码正确");
} else {
alert("密码错误");
agen.value = "";
}
}
btn.onclick = function() {
if (username.value.length == 0 || password.value.length == 0 || agen.value.length == 0) {
alert("请检查输入的内容");
} else {
console.log("ok");
}
};
</script>
</body>
</html>2回答
好帮手慕久久
2021-12-31
同学你好,代码正确,写成这样就可以,不需要优化了。
建议把样式调整一下,巩固一下布局的能力。
祝学习愉快!
听的说
提问者
2021-12-30
代码样式是测试阶段。没有按照上面样子做,请老师帮我找代码bug,以及优化地方。