js正则表达--表单验证

 

<style>
    * {
        margin: 0 auto;
        padding: 0;
    }

    form {
        width: 350px;
        height: 400px;
    }

    p span {
        display: inline-block;
        width: 80px;
        height: 22px;
        text-align: right;
    }

    p input {
        width: 220px;
        height: 20px;
        outline: none;
        border: none;
        border-bottom: 1px dashed black;
        margin-top: 10px;
        font-size: 10px;
    }

    p:nth-last-child(2) {
        width: 200px;
    }

    p:last-child {
        text-align: center;
    }

    p #code {
        float: left;
        margin-top: 10px;
        background-color: rgba(128, 128, 128, 0.514);
        text-align: center;
    }

    p #myCode {
        float: right;
        width: 80px;
        text-align: center;
    }
</style>

<body>
    <form action="">
        <p><span>用户名:</span><input type="text" id="username" placeholder="字母数字下划线组成,首字母不能为数字"></p>
        <p><span>密码:</span><input type="password" id="psw" placeholder="密码长度6-12位"></p>
        <p><span>确认密码:</span><input type="password" id="psw2" placeholder="请再次输入密码"></p>
        <p><span id="code"></span><input type="text" id="myCode" placeholder="不区分大小写"></p>
        <p><input type="button" value="注册" id="btn"></p>
    </form>


    <script>
        var username = document.getElementById('username');
        var psw = document.getElementById('psw');
        var psw2 = document.getElementById('psw2');
        var code = document.getElementById('code');
        var myCode = document.getElementById('myCode');
        var btn = document.getElementById('btn');
        var judgeName = false, judgePsw = false, judgePsw2 = false, judeCode = false;
        //用户名 首字母不能是数字,数字字母下划线组成
        var nameRule = /^[a-zA-Z_][\w]{0,}$/;
        username.addEventListener('change', function () {
            // console.log(this.value);
            if (nameRule.test(this.value)) {
                this.style.backgroundColor = 'green';
                judgeName = true;
            } else {
                this.style.backgroundColor = 'red';
                judgeName = false;
            }
        });
        //密码6-12位
        var pswRule = /^[\w\W]{6,12}$/;
        var psw0 = '';
        psw.addEventListener('change', function () {
            psw0 = this.value;
            if (pswRule.test(this.value)) {
                this.style.backgroundColor = 'green';
                judgePsw = true;
            } else {
                this.style.backgroundColor = 'red';
                judgePsw = false;
            }
        });
        //再次输入密码是否一致
        psw2.addEventListener('change', function () {
            // var psw2Rule =/psw0/;
            if (psw0 == this.value) {
                this.style.backgroundColor = 'green';
                judgePsw2 = true;
            } else {
                this.style.backgroundColor = 'red';
                judgePsw2 = false;
            }
        });
        //验证码
        var newCodeStr = '';
        for (var i = 48; i <= 57; i++) {
            newCodeStr += String.fromCharCode(i);
        }
        for (var i = 65; i <= 90; i++) {
            newCodeStr += String.fromCharCode(i);
        }
        for (var i = 97; i <= 122; i++) {
            newCodeStr += String.fromCharCode(i);
        }
        // console.log(newCode);
        var newCode = '';
        createCode();
        function createCode () {
            newCode = '';
            for (var i = 0; i < 4; i++) {
                var num = Math.floor(Math.random() * newCodeStr.length);
                newCode += newCodeStr[num];
            }
            code.innerText = newCode;
        }
        //判断验证码
        myCode.addEventListener('change', function () {
            if (newCode.toLocaleLowerCase() == this.value.toLocaleLowerCase()) {
                this.style.backgroundColor = 'green';
                judeCode = true;
            } else {
                this.style.backgroundColor = 'red';
                judeCode = false;
            }
        });
        //注册按钮
        btn.addEventListener('click', function () {
            if (judeCode && judgeName && judgePsw && judgePsw2) {
                alert('登陆中...');
            } else {
                alert('请修改不合法数据!');
            }
        })
    </script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoolPomelo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值