一个简单的表单验证

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>

    <style type="text/css">
        #div01 {
            border: 1px solid aquamarine;
            height: 600px;
            width: 600px;
            margin: auto;
            margin-top: 50px;
        }

        span {
            color: red;
        }
    </style>

    <body>
        <div id="div01">

            <form action="log.html">
                用 户 名:<input type="text" id="username" placeholder="请输入用户名">
                <span id='state1'></span><br/><br/> 密码:
                <input type="password" id="userpass" placeholder="请输入密码">
                <span id='state2'></span><br/><br/> 重复密码:
                <input type="password" id="userpass2" placeholder="请再次输入密码">
                <span id='state3'></span><br/><br/> 昵称:
                <input type="text" id="name" placeholder="请输入昵称">
                <span id='state4'></span><br/><br/> 出生日期:
                <input type="text" id="time" placeholder="请输入你出生日期">
                <span id='state5'></span><br/><br/> 性别:
                <input type="radio" id="sex"><input type="radio" id="sex"><br />
                <span id='state6'></span><br/><br/>身份证号码:
                <input type="text" id="t" placeholder="请输入身份证号码">
                <span id='state7'></span><br/><br/> 手机号码:
                <input type="text" id="repass" placeholder="请输入手机号码">
                <span id='state8'></span><br/><br/> QQ:
                <input type="text" id="QQ" placeholder="请输入手机号码">
                <span id='state9'></span><br/><br/> 邮  箱:
                <input type="email" id="em" placeholder="请输入邮箱">
                <span id='state10'></span><br/><br/>

                <input type="submit" value="注册" name="sub" />
                <input type="reset" value="重置" style="margin-left: 50px;" />
            </form>
        </div>

        <script type="text/javascript">
            var flas = true; //标识符    
            //账号验证  
            $("#username").blur(function() {
                //取出账号值  
                var name = $("#username").val();
                var state1 = $("#state1");
                //      账号必须为字母+数字的组合,不能出现特殊字符  
                varreg = /^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/;
                //alert(varreg.test(name))  

                if(name == null || name == "") {
                    state1.html("不能为空")
                    flas = false;
                    return
                } else {
                    flas = true;
                }

                if(!varreg.test(name)) {
                    state1.html("必须是字母+数字不能出现特殊字符")

                    flas = false;
                    return
                } else {
                    state1.html("✔")
                    flas = true;
                }
            })

            //密码验证  
            var flas1 = true; //标识符  
            $("#userpass").blur(function() {
                var pass = $("#userpass").val();
                var state2 = $("#state2"); //
                //密码规则为首字母大写+字母+数字的组合,不得出现特殊字符  
                var patrm1 = /^[A-Z][a-z0-9]*$/;
                if(pass == null || pass == "") {
                    state2.html("不能为空")
                    flas1 = false;
                    return
                } else {
                    flas1 = true;
                }

                if(!patrm1.test(pass)) {
                    state2.html("密码规则为首字母大写+字母+数字的组合,不得出现特殊字符。")

                    return
                } else {
                    state2.html("✔")
                    flas1 = true;
                }

            })

            //重复密码验证  
            var flas2 = true;
            $("#userpass2").blur(function() {
                var pass1 = $("#userpass").val(); //  
                var pass2 = $("#userpass2").val();
                var state3 = $("#state3"); //
                if(pass2 == null || pass2 == "") {
                    state3.html("不能为空")
                    flas2 = false;
                    return
                } else {
                    flas2 = true;
                }

                if(pass2 != pass1) {
                    state3.html("密码不一样")
                    flas2 = false;
                    return
                } else {
                    state3.html("✔")
                    flas2 = true;
                }

            })

            //昵称验证  
            var flas3 = true;
            $("#name").blur(function() {
                var nc = $("#name").val();
                var state4 = $("#state4"); //
                if(nc == null || nc == "") {
                    state4.html("不能为空")
                    flas3 = false;
                    return
                } else {
                    flas3 = true;
                }

                if(nc.indexOf("我是昵称1") > -1 || nc.indexOf("我是昵称2") > -1) {
                    state4.html("昵称重复的")
                    flas3 = false;
                    return
                } else {
                    state4.html("✔")
                    flas3 = true;
                }

            })

            //日期验证  
            var flas4 = true;
            $("#time").blur(function() {
                var rq = $("#time").val();
                var state5 = $("#state5");
                if(rq == null || rq == "") {
                    state5.html("不能为空")
                    flas4 = false;
                    return
                } else {
                    state5.html("✔")
                    flas4 = true;
                }

            })

            //性别验证  
            var flas9 = true; //标识符  
            $("#sex").blur(function() {
                var sex = $("#sex").val();
                var state6 = $("#state6");
                if(sex == null || sex == "") {
                    state6.html("不能为空")
                    flas9 = false;
                    return;
                } else {
                    state6.html("✔")
                    flas9 = true;
                }

            })

            //身份证验证  
            var flas5 = true; //标识符  
            $("#t").blur(function() {
                var sfz = $("#t").val();
                var state7 = $("#state7"); //
                //判断是纯数据  
                var aa = /^\d+$/

                if(sfz == null || sfz == "") {
                    state7.html("不能为空")
                    flas5 = false;
                    return

                } else {
                    flas5 = true;
                }

                if(!aa.test(sfz) || sfz.length != 18) {
                    state7.html("必须是纯数字并且不能小于18位")
                    flas5 = false;

                } else {
                    state7.html("✔")
                    flas5 = true;
                }

            })

            //手机号验证  
            var flas6 = true; //标识符  
            $("#repass").blur(function() {
                var tell1 = $("#repass").val();
                var state8 = $("#state8");
                //
                //以1开头的正则表达式  
                var aa = /^1\d{10}$/

                if(tell1 == null || tell1 == "") {
                    state8.html("不能为空")
                    flas6 = false;

                } else {
                    flas6 = true;
                }

                if(!aa.test(tell1) || tell1.length != 11) {
                    state8.html("必须是纯数字11位1开头")
                    flas6 = false;
                    return;
                } else {
                    state8.html("✔")
                    flas6 = true;
                }

            })

            //QQ验证  
            var flas7 = true; //标识符  
            $("#QQ").blur(function() {
                    var qq = $("#QQ").val();
                    var state9 = $("#state9");
                    //
                    var aa = /^\d+$/;
                    if(qq == null || qq == "") {
                        state9.html("不能为空")
                        flas7 = false;
                        return;
                    } else {
                        flas7 = true;
                    }

                    if(qq.length < 5) {
                        state9.html("必须是大于5位")
                        flas7 = false;
                        return;
                    } else {
                        state9.html("✔")
                        flas7 = true;
                    }

                })


                //邮箱验证
            var flas8 = true; //标识符  
            $("#em").blur(function() {
                var em = $("#em").val();
                var state10 = $("#state10");
                //邮箱的正则表单达式   
                var aa = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
                if(em == null || em == "") {
                    state10.html("不能为空")
                    flas8 = false;
                    return;
                } else {
                    state10.html("✔")
                    flas8 = true;
                }

                if(!aa.test(em)) {
                    state10.html("邮箱格式不正确")
                    flas8 = false;
                    return;
                } else {
                    state10.html("✔")
                    flas8 = true;
                }

            })


            //注册
            $("#submit").click(function() {
                //判断标识符都是true   
                if(flas9 == true && flas8 == true && flas == true && flas1 == true && flas2 == true && flas3 == true && flas4 == true && flas5 == true && flas6 == true && flas7 == true) {

                    alert("注册成功")
                } else {
                    alert("注册失败")
                }

            })
        </script>
    </body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值