表单验证—正则表达式验证表单

一、表单

<body>
<section id="register">
<div><img src="images/logo.jpg" alt="logo" /><img src="images/banner.jpg" alt="banner" /></div>
    <h1 class="hr_1">新用户注册</h1>
    <form action="register_success.htm" method="post" name="myform" id="form1">
        <dl>
            <dt>用户名:</dt>
            <dd><input id="user" type="text" /><div id="user_prompt">用户名由英文字母和数字组成的4-16位字符,以字母开头</div></dd>
        </dl>
        <dl>
            <dt>密码:</dt>
            <dd><input id="pwd" type="password"  /><div id="pwd_prompt">密码由英文字母和数字组成的4-10位字符</div></dd>
        </dl>
        <dl>
            <dt>确认密码:</dt>
            <dd><input id="repwd" type="password"/><div id="repwd_prompt"></div></dd>
        </dl>
        <dl>
            <dt>电子邮箱:</dt>
            <dd><input id="email" type="text"/><div id="email_prompt"></div></dd>
        </dl>
        <dl>
            <dt>手机号码:</dt>
            <dd><input id="mobile" type="text" /><div id="mobile_prompt"></div></dd>
        </dl>
        <dl>
            <dt>生日:</dt>
            <dd><input id="birth" type="text"/><div id="birth_prompt"></div></dd>
        </dl>
        <dl>
            <dt>&nbsp;</dt>
            <dd><input name=""  id="image" type="image" src="images/register.jpg" class="btn" /></dd>
        </dl>
  </form>
</section>
</body>

 

二、js校验

/**
 * Created by Administrator on 
 */
$(function () {
    var isGo=false;

    //用户名验证
    $("#user").blur(function () {
        var user = $("#user").val();
        var reg = /^[a-zA-Z][a-zA-Z0-9]{3,15}$/;
        if(reg.test(user)){
            $("#user_prompt").text("");
            isGo=true;
        }else{
            $("#user_prompt").text("你的输入格式有误!");
            isGo=false;
        }
    });


    //验证密码1
    $("#pwd").blur(function () {
        var pwd = $("#pwd").val();
        var reg = /^[a-zA-Z0-9]{4,10}$/;
        if(reg.test(pwd)){
            $("#pwd_prompt").text("");
            isGo=true;
        }else{
            $("#pwd_prompt").text("你的输入格式有误!");
            isGo=false;
        }
    });

    //验证密码2
    $("#repwd").blur(function () {
        var pwd = $("#pwd").val();
        var repwd = $("#repwd").val();
        var reg = /^[a-zA-Z0-9]{4,10}$/;
        if(!reg.test(repwd)){
            // $("#repwd_prompt").text("");
            // isGo=true;
            $("#repwd_prompt").text("你的输入格式有误!");
            isGo=false;
        }else if(pwd!=repwd){
            $("#repwd_prompt").text("两次输入的密码不一致!");
            isGo=false;
        }else{
            $("#repwd_prompt").text("");
            isGo=true;
        }
    });

    //验证手机号码
    $("#mobile").blur(function () {
        var mobile = $("#mobile").val();
        var reg = /^[1][0-9]{10}$/;
        if(reg.test(mobile)){
            $("#mobile_prompt").text("");
            isGo=true;
        }else{
            $("#mobile_prompt").text("你的输入有误!");
            isGo=false;
        }
    });

    //验证年龄
    $("#birth").blur(function () {
        var birth = $("#birth").val();
        var reg = /^(?:19\d{2}|200\d)-(?:1[0-2]|0?[0-9])-(?:3[01]|[12][0-9]|0?[0-9])$/;
        if(reg.test(birth)){
            $("#birth_prompt").text("");
            isGo=true;
        }else{
            $("#birth_prompt").text("你的输入有误!");
            isGo=false;
        }
    });


 function check() {
     return isGo;
 }

$("#image").click(function () {
    $("#form1").submit(check());
});

});

 

posted @ 2017-12-18 22:03  老人与JAVA  阅读(590)  评论(0)    收藏  举报