表单验证(正则表达式)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0 auto;
        }
        button{
            display: block;
            margin: 0 auto;
        }
        input{
            background-color: #c7edcc;
        }
        /*td{*/
            /*border: 1px solid #000;*/
        /*}*/
        img{
            width: 20px;
            height: 20px;
        }
        table,td{
            /*边框合并*/
            border-collapse: collapse;
            border: 1px solid #000;
        }

    </style>
    <script src="jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            $("tr td:first-child").css("background"," #c7edcc");
            $("span").hide();
            var x=0;
            //邮箱
            var email = $(".email").val();
            $(".email").blur(function () {
                var ereg = /.+@.+\.[a-zA-Z]{2,4}$/;
                  if(ereg.test($(".email").val())){
                      $(this).parents("td").next().children("span:last").hide();
                      $(this).parents("td").next().children("span:first").show();
                      x++;
                  }else{
                      $(this).parents("td").next().children("span:first").hide();
                      $(this).parents("td").next().children("span:last").show();
                      console.log(1);
                  }
            });
            //昵称
            var name = $(".name").val();
            $(".name").blur(function () {
                 if($(".name").val().length>4){
                     $(this).parents("td").next().children("span:last").hide();
                     $(this).parents("td").next().children("span:first").show();
                     x++;
                  }else{
                     $(this).parents("td").next().children("span:first").hide();
                     $(this).parents("td").next().children("span:last").show();
                  }
            });
            //密码
            var pwd = $(".pwd").val();
            $(".pwd").blur(function () {
                  if( $(".pwd").val().length>6){
                      $(this).parents("td").next().children("span:last").hide();
                      $(this).parents("td").next().children("span:first").show();
                      x++;
                  }else{
                      $(this).parents("td").next().children("span:first").hide();
                      $(this).parents("td").next().children("span:last").show();
                  }
            });
//            再一次输入
            var rpwd = $(".rpwd").val();
                $(".rpwd").blur(function () {
                  if($(".rpwd").val()!=$(".pwd").val()){
                      $(this).parents("td").next().children("span:first").hide();
                      $(this).parents("td").next().children("span:last").show();
                        x++;
                  }else{
                      $(this).parents("td").next().children("span:last").hide();
                      $(this).parents("td").next().children("span:first").show();

                  }
            });

            $(".zhuce").click(function () {
                if(x==4){
                    alert("符合要求");
                }else{
                    alert("不符合要求");
                }
            })
        })
    </script>
</head>
<body>

    <!--行内实线<table cellspacing="0" cellpadding="0.1" border="1px">-->
    <table>
        <tr>
            <td>请填写您的Email地址:</td>
            <td><input type="text" class="email"></td>
            <td>请填写有效的Email地址,在下一步中您将用此邮箱接收验证邮件。<br>
                <span><img src="img/dui.gif">用户名可用</span><span><img src="img/cuo.gif">用户名不可用</span></td>
        </tr>
        <tr>
            <td>请填写您在当当网的昵称:</td>
            <td><input type="text" class="name"></td>
            <td>您的昵称可以由小写英文字母,中文、数组组合才能,长度4-20个字符,一个汉字为两个字符<br>
                <span><img src="img/dui.gif">昵称可用</span><span><img src="img/cuo.gif">昵称不可用</span></td>
        </tr>
        <tr>
            <td>设置密码:</td>
            <td><input type="text" class="pwd"></td>
            <td>您的密码可由大小写英文字母、数字组成,长度6-20位<br>
                <span><img src="img/dui.gif">密码可用</span><span><img src="img/cuo.gif">密码不可用</span></td>
        </tr>
        <tr>
            <td>再次输入密码:</td>
            <td><input type="text" class="rpwd"></td>
            <td>
                <span><img src="img/dui.gif">密码一致</span><span><img src="img/cuo.gif">密码不一致</span></td>
        </tr>
    </table>
<button class="zhuce">注册</button>
</body>
</html>
 

猜你喜欢

转载自blog.csdn.net/Mydtudysy/article/details/78051736
今日推荐