麻烦老师批阅,看看是否可以优化,谢谢

麻烦老师批阅,看看是否可以优化,谢谢

<!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>
    <style>
       
        .warning{
            display: none;
        }
        #nameWarning {
            color: orange;
        }

        .p2 {
            position: relative;
        }

        ul {
            list-style: none;
            position: absolute;
            left: 243px;
            top: 50px;
        }
        ul li {
            height: 5px;
            width: 40px;
            border: 1px solid #000;
            float: left;
            margin-right: 10px;
            background-color: rgba(149, 144, 144, 0.5);
        }
        #errorbox {
            position: absolute;
            top: 78px;
            left: 95px;
            font-size: 13px;
            display: none;
        }
       

       
    </style>
</head>
<body>
    <div class="box">
        <p>
            用户名:
            <input type="text" id="nameField" placeholder="用户名设置后不可修改">
            <span  id="nameWarning">6-30位字母、数字或’_’,字母开头</span>
        </p>
        <p class="p2"> 
            登录密码:
            <input type="password" id="pwdField" placeholder="6-20位字母或数字">
            <div id="errorbox">6-20位字母或数字组成</div>
            <ul id="list">
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </p>
        <p>
            确认密码:
            <input type="password" id="qdField" placeholder="请再次输入您的密码">
            <span class="warning" id="qd1">两次输入一致</span>
            <span class="warning" id="qd2">输入框不能为空</span>
            <span class="warning" id="qd3">两次密码输入不一致</span>
        </p>
        <p>
            <button id="btn" >注册</button>
        </p>
    </div>

    <script>

        //用户名事件
        var nameField = document.getElementById('nameField');
        var nameWarning = document.getElementById('nameWarning');

        nameField.onblur = function () {
            var name = nameField.value;
            var regexp1 = /^[a-zA-z]\w{5,30}$/;

            if (regexp1.test(name)) {
                nameWarning.innerText = '用户名输入正确';
                nameWarning.style.color = 'green';
            }else if (name.length == 0) {
                nameWarning.innerText = '6-30位字母、数字或’_’,字母开头';
                nameWarning.style.color = 'orange';
            }else {
                nameWarning.innerText = '6-30位字母、数字或’_’,字母开头';
                nameWarning.style.color = 'red';
            }
        }


        //密码输入事件
        var pwdField = document.getElementById('pwdField');
        var pwdWarning = document.getElementById('pwdWarning');
        var errorbox = document.getElementById('errorbox');
        var list = document.getElementById('list');
        var lis = document.querySelectorAll('#list li');

        pwdField.onblur = function () {
             
            var password = pwdField.value;
            var pwd1 = /^[0-9]{6,20}$|^[a-z]{6,20}$|^[A-Z]{6,20}$/;
            var pwd2 = /^[a-z0-9]{6,20}$|^[A-Z0-9]{6,20}$|^[A-Za-z ]{6,20}$/;
            var pwd3 = /^[A-Za-z0-9]{6,20}$/;

            if (pwd1.test(password)) {
                errorbox.style.display = 'none';
                lis[0].style.backgroundColor = 'red';
                lis[1].style.backgroundColor = 'rgba(149, 144, 144, 0.5)';
                lis[2].style.backgroundColor = 'rgba(149, 144, 144, 0.5)';
            }else if (pwd2.test(password)) {
                errorbox.style.display = 'none';
                lis[0].style.backgroundColor = 'red';
                lis[1].style.backgroundColor = 'orange';
                lis[2].style.backgroundColor = 'rgba(149, 144, 144, 0.5)';
            }else if (pwd3.test(password)) {
                errorbox.style.display = 'none';
                lis[0].style.backgroundColor = 'red';
                lis[1].style.backgroundColor = 'orange';
                lis[2].style.backgroundColor = 'green';
            }else {
                errorbox.style.display = 'inline';
                errorbox.style.color = 'red';
                lis[0].style.backgroundColor = 'rgba(149, 144, 144, 0.5)';
                lis[1].style.backgroundColor = 'rgba(149, 144, 144, 0.5)';
                lis[2].style.backgroundColor = 'rgba(149, 144, 144, 0.5)';
            }
        }


        //确认密码输入框
        var qdField = document.getElementById('qdField');
        var password = pwdField.value;
        var qd1 = document.getElementById('qd1');
        var qd2 = document.getElementById('qd2');
        var qd3 = document.getElementById('qd3');

       
        qdField.onblur = function () {
            var password = pwdField.value;
            var appwd = qdField.value;

            if (appwd.length == 0 && password.length == 0) {
                qd1.style.display = 'none';
                qd2.style.display = 'none';
                qd3.style.display = 'none';
            }else if (appwd == password) {
                qd1.style.display = 'inline';
                qd2.style.display = 'none';
                qd3.style.display = 'none';
                qd1.innerText = '两次输入一致';
                qd1.style.color = 'green';
            }else if (appwd.length == 0){
                qd1.style.display = 'none';
                qd2.style.display = 'inline';
                qd3.style.display = 'none';
                qd2.innerText = '输入框不能为空';
                qd2.style.color = 'red';
            }else{
                qd1.style.display = 'none';
                qd2.style.display = 'none';
                qd3.style.display = 'inline';
                qd3.innerText = '两次密码输入不一致';
                qd3.style.color = 'red';
            }
        }

        //注册按钮事件
        var btn = document.getElementById('btn');

        btn.onclick = function () {
            var name = nameField.value;
            var regexp1 = /^[a-zA-z]\w{5,30}$/;

            var password = pwdField.value;
            var pwd1 = /^[0-9]{6,20}$|^[a-z]{6,20}$|^[A-Z]{6,20}$/;
            var pwd2 = /^[a-z0-9]{6,20}$|^[A-Z0-9]{6,20}$|^[A-Za-z ]{6,20}$/;
            var pwd3 = /^[A-Za-z0-9]{6,20}$/;

            var appwd = qdField.value;

            if (regexp1.test(name) && (pwd1.test(password)||pwd2.test(password)||pwd3.test(password)) && appwd==password) {
                alert('信息填写正确');
            }else {
                alert('请填写正确的信息');
            }
        }
    </script>
</body>
</html>


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

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

1回答
好帮手慕久久 2022-10-29 09:42:04

同学你好,代码是对的。注册按钮中的逻辑可以优化一下。建议设置变量,保存每一项的验证结果,注册按钮中直接用该变量判断就行,这样注册按钮中,不用再做验证了。以用户名例如:

https://img1.sycdn.imooc.com//climg/635c848a09c2f6ef10690787.jpg

其余两项同样处理。按钮中,就可以使用这三个变量了:

https://img1.sycdn.imooc.com//climg/635c84e3092295c210160592.jpg

祝学习愉快!

  • 提问者 啊聪聪 #1

    好的,谢谢老师

    2022-10-29 10:35:15
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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