请老师帮忙检查一下代码

请老师帮忙检查一下代码

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>表单验证</title>

</head>

<body>
<div class="box">
<p><b>*</b>用 户&nbsp; 名:
<input type="text" placeholder="用户名设置成功后不可修改" id="nameinp">
<span class="asxd" id="asxd">6-30位字母、数字或_,字母开头</span>
</p>
</div>
<div class="box enter">
<p><b>*</b>登入密码:
<input type="password" placeholder='6-20位字母或数字' id="passinp">
<span id="redbox" class="box2"></span>
<span id="yellowbox" class="box2"></span>
<span id="greenbox" class="box2"></span><br>
<span class='tishi' id="tishi">6-20位字母或数字</span>
</p>

</div>
<div class="box">
<p><b>*</b>确认密码:
<input type="password" placeholder='再次输入您的登入密码' id="yeinp">
<span id="tishi1"></span>
</p>
</div>
<div class="box1">
<button id="button">注册</button>
</div>

</body>

</html>
 <style>
.box {
height: 50px;
}

.box input {
width: 220px;
height: 30px;
border-radius: 5px;
border: 1px solid #ccc;
}

.box b {
color: red;
}

.box1 {
margin-top: 60px;
margin-left: 160px;
}

.box1 button {
width: 230px;
height: 40px;
background-color: orange;
color: white;
text-align: center;
line-height: 40px;
font-size: 18px;
border: none;
border-radius: 5px;
}

.box .asxd {
color: rgba(255, 166, 0, 0.795);
margin-left: 15px;
}

.box .tishi {
font-size: 12px;
color: red;
margin-left: 100px;
display: none;
}

.enter {
width: 600px;
position: relative;
}

.enter .box2 {
display: inline-block;
width: 50px;
height: 7px;
background-color: #ccc;
margin-left: 10px;
}
</style>
<script>
var nameinp = document.getElementById('nameinp');
var asxd = document.getElementById('asxd');
var passinp = document.getElementById('passinp')
var yeinp = document.getElementById('yeinp')
var tishi = document.getElementById('tishi')
var redbox = document.getElementById('redbox')
var yellowbox = document.getElementById('yellowbox')
var greenbox = document.getElementById('greenbox')
var tishi1 = document.getElementById('tishi1')
var button= document.getElementById('button')
// 用户名离开事件
nameinp.onblur = function () {
var xinxi = nameinp.value;
if (/^[a-zA-Z]\w{5,29}$/.test(xinxi)) {
asxd.innerText = '用户名输入正确'
asxd.style.color = 'green';
} else {
asxd.innerText = '6-30位字母、数字或_,字母开头'
asxd.style.color = 'red';
}
}
// 登入密码事件
passinp.onblur = function () {
yellowbox.style.backgroundColor = '#ccc';
redbox.style.backgroundColor = '#ccc';
greenbox.style.backgroundColor = '#ccc';
tishi.style.display = 'none';
var xinxi = passinp.value;
if (/^[a-zA-Z0-9]{6,20}$/.test(xinxi)) {
if (/^[0-9]+$|^[a-z]+$|^[A-Z]+$/.test(xinxi)) {
redbox.style.backgroundColor = 'red';
} else if (/^[a-z0-9]+$|^[A-Z0-9]+$|^[a-zA-Z]+$/.test(xinxi)) {
yellowbox.style.backgroundColor = 'yellow';
redbox.style.backgroundColor = 'red';
} else if (/^[a-z0-9A-Z]+$/.test(xinxi)) {
yellowbox.style.backgroundColor = 'yellow';
redbox.style.backgroundColor = 'red';
greenbox.style.backgroundColor = 'green';
}

} else {
tishi.style.display = 'inline';
}

}
// 密码确认框
yeinp.onblur = function () {
var xinxi = yeinp.value;
if (xinxi) {
if (passinp.value == xinxi) {
tishi1.innerText = '两次输入一致';
tishi1.style.color = 'green';
} else {
tishi1.innerText = '两次密码输入不一致,请重新输入';
tishi1.style.color = 'red';
}
} else {
tishi1.innerText = '输入框不能为空';
tishi1.style.color = 'red';
}
}
button.onclick=function(){
if(/^[a-zA-Z]\w{5,29}$/.test(nameinp.value)&&passinp.value == yeinp.value){
alert('信息填写正确');
}else{
alert('请填写信息正确');
}
}
</script>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕言 2021-06-27 14:31:23

同学你好,代码写的很简洁了,不用优化了,祝学习愉快~

好帮手慕言 2021-06-27 11:17:41

同学你好,效果是对的,继续加油,祝学习愉快~

  • 提问者 激情的樱木花道 #1

    代码有些繁琐,老师给看一下还能不能优化。

    2021-06-27 12:19:04
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师