使用jquery对邮箱,手机号码,身份证号的验证

$(function () {
    var state = false;
    $("#email").focus(function () {   //对邮箱的验证
        if (state == false) {
            $(this).val('');
        }
    })
    $("#email").blur(function () {
        if ($(this).val() != '') {
            if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(this).val()) == false) {
             $("#emailInfo").text("邮箱格式不正确,请重新填写");
             $("#emailInfo").append("<img src=images/error.gif />");
                $(this).focus();
            }
            else {
             $("#emailInfo").text('');
                $("#emailInfo").append("<img src=images/onSuccesss.jpg />");
                state=true;
            }
        }
    })

     $("#mobile").focus(function () {   //对手机号码的验证
        if (state == false) {
            $(this).val('');
        }
    })
    $("#mobile").blur(function () {
        if ($(this).val() != '') {
            if (/^1[3|4|5|8][0-9]\d{4,8}$/.test($(this).val()) == false) {
             $("#mobileInfo").text("电话格式不正确,请重新填写例如:13415764179或0321-4816048");
             $("#mobileInfo").append("<img src=images/error.gif />");
                $(this).focus();
            }
            else {
             $("#mobileInfo").text('');
                $("#mobileInfo").append("<img src=images/onSuccesss.jpg />");
                state=true;
            }
        }
    })

    $("#idCardin").focus(function () {        //对身份证号的验证,注意不要与js文件红的名字产生冲突
        if (state == false) {
            $(this).val('');
        }
    })
    $("#idCardin").blur(function () {
        if ($(this).val() != '') {
            if (/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/.test($(this).val()) == false) {
             $("#idCardInfo").text("身份证格式不正确,请重新输入18位的身份证号!");
             $("#idCardInfo").append("<img src=images/error.gif />");
                $(this).focus();
            }
            else {
             $("#idCardInfo").text('');
                $("#idCardInfo").append("<img src=images/onSuccesss.jpg />");
                state=true;
            }
        }
    })
});

posted on 2013-06-19 08:53  火玉  阅读(563)  评论(0编辑  收藏  举报