码迷,mamicode.com
首页 > 其他好文 > 详细

12306中前端验证身份证件(及其他证件号)的方法

时间:2015-01-08 14:43:53      阅读:533      评论:0      收藏:0      [点我收藏+]

标签:

  之前做一个门票预约系统的时候,需要用到身份证件的验证功能,第一个想法就是去12306网站上去参考验证方法,于是开始了第一步:

  1.登录到12306网站->常用联系人->添加技术分享

试了一下,果然有证件号的验证,而且验证的情况还考虑比较周密。初步判断采用了什么验证插件。

2.Chrome下用开发人员工具查看JS代码

搜索关键词”身份证“,在一个JS文件中找到了:

技术分享

 

将JS代码Copy下来,百度个JS格式化工具,格式化后JS代码如下:

技术分享
jQuery.validator.addMethod("checkLoginUserName",
function (j, g) {
    var i = false;
    var h = /^[A-Za-z]{1}([A-Za-z0-9]|[_]){0,29}$/;
    var f = /^((([a-z]|\d|[!#\$%&‘\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&‘\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
    if (h.test(j) || f.test(j)) {
        i = true
    }
    return this.optional(g) || i
},
"wrong username.");
jQuery.validator.addMethod("requiredUserName",
function (c, d) {
    if ("用户名/邮箱" == c) {
        return false
    }
    if (c == null || "" == c) {
        return false
    }
    return true
},
"wrong username.");
jQuery.validator.addMethod("requiredSchoolName",
function (c, d) {
    if ("简码/汉字" == c) {
        return false
    }
    if (c == null || "" == c) {
        return false
    }
    return true
},
"wrong schoolname.");
jQuery.validator.addMethod("randCodeRequired",
function (c, d) {
    $("#i-ok").css("display", "none");
    return c.length > 0
},
"验证码错误!");
jQuery.validator.addMethod("randCodeFormat",
function (e, f) {
    $("#i-ok").css("display", "none");
    var d = /^[a-zA-Z0-9]+$/;
    return this.optional(f) || d.test(e)
},
"验证码错误!");
jQuery.validator.addMethod("randCodeLength",
function (c, d) {
    $("#i-ok").css("display", "none");
    return c.length == 4
},
"验证码错误!.");
jQuery.validator.addMethod("integrationCheck",
function (f, d) {
    var e = /^\d{6}$/;
    return this.optional(d) || e.test(f)
},
"wrong integrationpassword");
jQuery.validator.addMethod("integrationPwdCheck",
function (f, d, e) {
    if ($("#" + e[0]).val() == $("#" + e[1]).val()) {
        return true
    }
    return false
},
"两次输入密码不一致!.");
jQuery.validator.addMethod("checkRandCode",
function (f, g, e) {
    var h = true;
    if (f && f.length == 4) {
        $.ajax({
            url: ctx + "passcodeNew/checkRandCodeAnsyn",
            type: "post",
            data: {
                randCode: f,
                rand: e
            },
            async: false,
            success: function (a) {
                if (a.data == "N") {
                    h = false;
                    $("#i-ok").css("display", "none")
                } else {
                    h = true;
                    $("#i-ok").css("display", "block")
                }
            }
        })
    } else {
        h = false;
        $("#i-ok").css("display", "none")
    }
    return h
},
"验证码错误!.");
jQuery.validator.addMethod("validateUsersName",
function (c, d) {
    return this.optional(d) || /^[A-Za-z]{1}([A-Za-z0-9]|[_]){0,29}$/.test(c)
},
"用户名只能由字母、数字或_组成");
jQuery.validator.addMethod("checkWriteSpace",
function (e, f) {
    for (var d = 0; d < e.length; d++) {
        if (e.charCodeAt(d) == 32) {
            return false
        }
    }
    return true
},
"contain writespace");
jQuery.validator.addMethod("validateRandCode",
function (c, d) {
    return this.optional(d) || /^[a-zA-Z0-9]+$/.test(c)
},
"验证码错误!.");
jQuery.validator.addMethod("checkPassward",
function (g, h, j) {
    var f = true;
    for (var i = 0; i < g.length; i++) {
        if (g.charCodeAt(i) == 39 || g.charCodeAt(i) == 60 || g.charCodeAt(i) == 62) {
            f = false
        }
        if (!f) {
            break
        }
    }
    return this.optional(h) || f
},
"Passward wrong");
function validateSecIdCard(n) {
    var d = 0;
    var l = n;
    var i = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙",
        21: "辽宁",
        22: "吉林",
        23: "黑龙",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    if (!/^\d{17}(\d|x)$/i.test(l)) {
        return false
    }
    l = l.replace(/x$/i, "a");
    if (i[parseInt(l.substr(0, 2))] == null) {
        return false
    }
    var j = l.substr(6, 4) + "-" + Number(l.substr(10, 2)) + "-" + Number(l.substr(12, 2));
    var m = new Date(j.replace(/-/g, "/"));
    if (j != (m.getFullYear() + "-" + (m.getMonth() + 1) + "-" + m.getDate())) {
        return false
    }
    for (var k = 17; k >= 0; k--) {
        d += (Math.pow(2, k) % 11) * parseInt(l.charAt(17 - k), 11)
    }
    if (d % 11 != 1) {
        return false
    }
    return true
}
function validateFirIdCard(n) {
    var d = 0;
    var l;
    var i = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙",
        21: "辽宁",
        22: "吉林",
        23: "黑龙",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    if (n.length == 15) {
        l = idCardUpdate(n)
    } else {
        l = n
    }
    if (!/^\d{17}(\d|x)$/i.test(l)) {
        return false
    }
    l = l.replace(/x$/i, "a");
    if (i[parseInt(l.substr(0, 2))] == null) {
        return false
    }
    var j = l.substr(6, 4) + "-" + Number(l.substr(10, 2)) + "-" + Number(l.substr(12, 2));
    var m = new Date(j.replace(/-/g, "/"));
    if (j != (m.getFullYear() + "-" + (m.getMonth() + 1) + "-" + m.getDate())) {
        return false
    }
    for (var k = 17; k >= 0; k--) {
        d += (Math.pow(2, k) % 11) * parseInt(l.charAt(17 - k), 11)
    }
    if (d % 11 != 1) {
        return false
    }
    return true
}
function idCardUpdate(m) {
    var k;
    var n = /^(\d){15}$/;
    if (n.test(m)) {
        var h = 0;
        var l = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var i = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
        m = m.substr(0, 6) + "19" + m.substr(6, m.length - 6);
        for (var j = 0; j < m.length; j++) {
            h += parseInt(m.substr(j, 1)) * l[j]
        }
        m += i[h % 11];
        k = m
    } else {
        k = "#"
    }
    return k
}
jQuery.validator.addMethod("checkBorth",
function (d, g) {
    var h = d;
    if (h == "") {
        return true
    } else {
        var i = h.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if (i == null) {
            return false
        }
        var j = new Date(i[1], i[3] - 1, i[4]);
        return (j.getFullYear() == i[1] && (j.getMonth() + 1) == i[3] && j.getDate() == i[4])
    }
},
"日期格式不合法");
jQuery.validator.addMethod("byteRangeLength",
function (f, h, j) {
    var g = f.length;
    for (var i = 0; i < f.length; i++) {
        if (f.charCodeAt(i) > 127) {
            g++
        }
    }
    return this.optional(h) || (g >= j[0] && g <= j[1])
},
"length wrong");
jQuery.validator.addMethod("checkNameCharBlank",
function (f, g, e) {
    var h = e.split("@");
    if ($("#" + h[1]).val() == "") {
        return true
    } else {
        if ($("#" + h[0]).val() == "1" || $("#" + h[0]).val() == "2") {
            return this.optional(g) || /^[a-zA-Z·..\u3400-\u9FFF]+$/.test(f)
        } else {
            if ($("#" + h[0]).val() == "B" || $("#" + h[0]).val() == "H") {
                if (/^[-]+$/.test(f)) {
                    return false
                }
                return this.optional(g) || /^[a-z A-Z·..\u3400-\u9FFF\-]+$/.test(f)
            } else {
                return this.optional(g) || /^[a-z A-Z·..\u3400-\u9FFF]+$/.test(f)
            }
        }
    }
},
"wrong name.");
jQuery.validator.addMethod("checkIdValidStr",
function (e, f) {
    var d = /^[a-zA-Z0-9\_\-\(\)]+$/;
    return this.optional(f) || (d.test(e))
},
"wrong id");
jQuery.validator.addMethod("isSecIDCard",
function (f, d, e) {
    if (!checkIfSecIdCard($(e).val())) {
        return true
    }
    return validateSecIdCard(f)
},
"wrong");
function checkIfSecIdCard(b) {
    if (b == "1") {
        return true
    }
    return false
}
function checkIfFirIdCard(b) {
    if (b == "2") {
        return true
    }
    return false
}
function checkCardForHKorTW(b) {
    if (b == "C" || b == "G") {
        return true
    }
    return false
}
jQuery.validator.addMethod("isFirIDCard",
function (f, d, e) {
    if (!checkIfFirIdCard($(e).val())) {
        return true
    }
    return validateFirIdCard(f)
},
"wrong");
jQuery.validator.addMethod("checkHkongMacao",
function (f, g, e) {
    if ($(e).val() == "C") {
        var h = /^[HMhm]{1}([0-9]{10}|[0-9]{8})$/;
        return this.optional(g) || (h.test(f))
    } else {
        return true
    }
},
"wrong format.");
jQuery.validator.addMethod("checkTaiw",
function (g, i, j) {
    if ($(j).val() == "G") {
        var f = /^[0-9]{8}$/;
        var h = /^[0-9]{10}$/;
        return this.optional(i) || (f.test(g)) || (h.test(g))
    } else {
        return true
    }
},
"wrong format.");
jQuery.validator.addMethod("checkPassport",
function (f, h, j) {
    if ($(j).val() == "B") {
        var g = /^[a-zA-Z]{5,17}$/;
        var i = /^[a-zA-Z0-9]{5,17}$/;
        return this.optional(h) || (i.test(f)) || g.test(f)
    } else {
        return true
    }
},
"wrong format.");
jQuery.validator.addMethod("checkWork",
function (f, h, j) {
    if ($(j).val() == "H") {
        var g = /^[a-zA-Z]{5,17}$/;
        var i = /^[a-zA-Z0-9]{5,17}$/;
        return this.optional(h) || (i.test(f)) || g.test(f)
    } else {
        return true
    }
},
"wrong format.");
jQuery.validator.addMethod("isMobile",
function (e, d) {
    var f = e.length;
    return this.optional(d) || (f == 11 && /^[0-9]+$/.test(e))
},
"wrong mobile phone ");
jQuery.validator.addMethod("isTelePhone",
function (f, d) {
    var e = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^[0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}#)/;
    return this.optional(d) || (e.test(f))
},
"wrong telePhone ");
jQuery.validator.addMethod("illegalChar",
function (g, h, j) {
    var f = true;
    if (g.indexOf("$") >= 0) {
        return false
    }
    for (var i = 0; i < g.length; i++) {
        if (g.charCodeAt(i) == 39 || g.charCodeAt(i) == 60 || g.charCodeAt(i) == 62 || g.charCodeAt(i) == 34 || g.charCodeAt(i) == 63) {
            f = false
        }
        if (!f) {
            break
        }
    }
    return this.optional(h) || f
},
"Illegal char wrong");
jQuery.validator.addMethod("isZipCode",
function (e, f) {
    var d = /^[0-9]{6}$/;
    return this.optional(f) || (d.test(e))
},
"wrong zipcode");
jQuery.validator.addMethod("isEmail",
function (e, d) {
    var f = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return this.optional(d) || (f.test(trim(e)))
},
"wrong email");
function replaceChar(c) {
    var d = c.value.replace(/[‘"<> ?]/g, "");
    c.value = d
}
function checkNameChar1(b) {
    return /^[a-zA-Z0-9\u3400-\u9FFF]+$/.test(b)
}
function trim(b) {
    return b.replace(/(^\s*)|(\s*$)/g, "")
}
function ltrim(b) {
    return b.replace(/(^\s*)/g, "")
}
function rtrim(b) {
    return b.replace(/(\s*$)/g, "")
}
jQuery.validator.addMethod("validateName",
function (c, d) {
    return this.optional(d) || /^[a-zA-Z\u3400-\u9FFF0-9\_]+$/.test(c)
},
"wrong username.");
jQuery.validator.addMethod("studentRequired",
function (f, d, e) {
    if ($(e).val() == "3") {
        return f && trim(f) != ""
    }
    return true
},
"wrong studentRequired.");
jQuery.validator.addMethod("studentStationRequired",
function (f, d, e) {
    if ($(e).val() == "3") {
        return f && trim(f) != "简拼/全拼/汉字" && trim(f) != ""
    }
    return true
},
"wrong studentStationRequired.");
jQuery.validator.addMethod("studentValidateName",
function (f, d, e) {
    if ($(e).val() == "3") {
        return this.optional(d) || /^[a-zA-Z\u3400-\u9FFF0-9\_]+$/.test(f)
    }
    return true
},
"wrong username.");
jQuery.validator.addMethod("checkStudentName",
function (f, d, e) {
    if ($(e).val() == "3") {
        if ((!f || trim(f) == "" || trim(f) == "简码/汉字")) {
            return false
        }
    }
    return true
},
"wrong username.");
jQuery.validator.addMethod("isQuestionNull",
function (f, d, e) {
    if (jQuery.trim(f) != "") {
        if (jQuery.trim($(e[0]).val()) == "customQuestion" && jQuery.trim($(e[1]).val()) == "" || jQuery.trim($(e[0]).val()) == "") {
            return false
        }
    }
    return true
},
"you should input the question");
jQuery.validator.addMethod("isAnswerNull",
function (f, d, e) {
    if ((jQuery.trim($(e[0]).val()) == "customQuestion" && jQuery.trim($(e[1]).val()) != "") || (jQuery.trim($(e[0]).val()) != "")) {
        if (jQuery.trim(f) == "") {
            return false
        }
    }
    return true
},
"you should input the answer");
function checkSex(e, f, d) {
    if (!checkSexByCardId(e, f, d)) {
        if (!confirm("性别与身份证中的性别不符,是否继续?")) {
            return false
        } else {
            return true
        }
    } else {
        return true
    }
}
function checkSexByCardId(g, j, i) {
    function h(a, e) {
        var b = null;
        if (e.length == 15) {
            b = e.substring(14, 15)
        } else {
            if (e.length == 18) {
                b = e.substring(16, 17)
            } else {
                return true
            }
        }
        if (b == "x" || b == "X") {
            b = "10"
        }
        var c = parseInt(b);
        var d = c % 2;
        if (d === 0 && a === "F") {
            return true
        } else {
            if (d === 1 && a === "M") {
                return true
            } else {
                return false
            }
        }
    }
    var f = $(i).val();
    if (checkIfSecIdCard($(j).val()) && validateSecIdCard(f)) {
        if (f !== "") {
            return h(g, f)
        } else {
            return true
        }
    } else {
        if (checkIfFirIdCard($(j).val()) && validateFirIdCard(f)) {
            if (f !== "") {
                return h(g, f)
            } else {
                return true
            }
        } else {
            return true
        }
    }
}
function checkBirdDateByCardId(g, j, h) {
    var i = null;
    var f = $(h).val();
    if (checkIfSecIdCard($(j).val()) && f !== "" && validateSecIdCard(f)) {
        i = f.substring(6, 14)
    } else {
        if (checkIfFirIdCard($(j).val()) && f !== "" && validateFirIdCard(f)) {
            if (f.length == 15) {
                i = "19" + f.substring(6, 12)
            } else {
                if (f.length == 18) {
                    i = f.substring(6, 14)
                }
            }
        } else {
            return true
        }
    }
    if (g !== "") {
        g = g.replace(/-/g, "");
        if (g != i) {
            return false
        } else {
            return true
        }
    } else {
        return true
    }
}
function checkBirdate(e, f, d) {
    if (!checkBirdDateByCardId(e, f, d)) {
        if (!confirm("出生日期与身份证中的出生日期不符,是否继续?")) {
            return false
        } else {
            return true
        }
    } else {
        return true
    }
}
jQuery.validator.addMethod("checkPwdValidate",
function (c, d) {
    return this.optional(d) || /(?![a-z]+$|[0-9]+$|_+$)^[a-zA-Z0-9_]{6,}$/.test(c)
},
"contain writespace");
jQuery.validator.addMethod("checkConfirmPassWard",
function (f, d, e) {
    if ($(e).val() != null) {
        return $(e).val() == f
    }
    return true
},
"contain writespace");
jQuery.validator.addMethod("IVR_passwd_format",
function (f, d) {
    var e = /^[0-9]{6}$/;
    return this.optional(d) || e.test(f)
},
"验证码错误!.");
jQuery.validator.addMethod("checkStation",
function (c, d) {
    if ((!c || trim(c) == "" || trim(c) == "简拼/全拼/汉字" || trim(c) == "简拼/全拼/汉字或↑↓")) {
        return false
    }
    return true
},
"wrong username.");
jQuery.validator.addMethod("checkAnsyUserName",
function (l, h, k) {
    var i = k[0];
    var g = $("#" + k[1]).val();
    var j = true;
    $.ajax({
        url: i + "?user_name=" + l,
        type: "get",
        async: false,
        success: function (b, a) {
            if (b.data == true) {
                j = false
            } else {
                j = true
            }
        },
        error: function (a, b, c) {
            j = false
        }
    });
    return j
},
"wrong cardNo");
function checkPwdRank(j, i, f) {
    var h = $(j);
    var g = h.val();
    if (g.length <= 6 || new RegExp("^[a-zA-Z]{6,}$").test(g) || new RegExp("^[0-9]{6,}$").test(g) || new RegExp("^[_]{6,}$").test(g)) {
        $("#" + i).attr("title", "危险");
        $("#" + f).html("危险");
        $("#" + i).removeClass("rank-a");
        $("#" + i).removeClass("rank-b");
        $("#" + i).removeClass("rank-c");
        $("#" + i).addClass("rank-a")
    } else {
        if (g.length > 6 && new RegExp("[a-zA-Z]").test(g) && new RegExp("[0-9]").test(g) && new RegExp("[_]").test(g)) {
            $("#" + i).attr("title", "安全");
            $("#" + f).html("安全");
            $("#" + i).removeClass("rank-a");
            $("#" + i).removeClass("rank-b");
            $("#" + i).removeClass("rank-c");
            $("#" + i).addClass("rank-c")
        } else {
            $("#" + i).attr("title", "一般");
            $("#" + f).html("一般");
            $("#" + i).removeClass("rank-a");
            $("#" + i).removeClass("rank-b");
            $("#" + i).removeClass("rank-c");
            $("#" + i).addClass("rank-b")
        }
    }
}
Array.prototype.unique = function () {
    var f = {},
    d = this.length;
    for (var e = 0; e < d; e++) {
        if (typeof f[this[e]] == "undefined") {
            f[this[e]] = 1
        }
    }
    this.length = 0;
    d = 0;
    for (var e in f) {
        this[d++] = e
    }
    return this
};
function checkSearchPwdRank(p, m, i) {
    var k = $(p);
    var j = k.val();
    if (j.length < 6) {
        $("#" + m).attr("title", "危险");
        $("#" + i).html("危险");
        $("#" + m).removeClass("rank-a");
        $("#" + m).removeClass("rank-b");
        $("#" + m).removeClass("rank-c");
        $("#" + m).addClass("rank-a")
    } else {
        var o = [];
        for (var n = 0; n < 6; n++) {
            o.push(j.charAt(n))
        }
        o = o.unique();
        var l = o.length;
        if (l == 1) {
            $("#" + m).attr("title", "危险");
            $("#" + i).html("危险");
            $("#" + m).removeClass("rank-a");
            $("#" + m).removeClass("rank-b");
            $("#" + m).removeClass("rank-c");
            $("#" + m).addClass("rank-a")
        } else {
            if (l > 1 && l < 5) {
                $("#" + m).attr("title", "一般");
                $("#" + i).html("一般");
                $("#" + m).removeClass("rank-a");
                $("#" + m).removeClass("rank-b");
                $("#" + m).removeClass("rank-c");
                $("#" + m).addClass("rank-b")
            } else {
                $("#" + m).attr("title", "安全");
                $("#" + i).html("安全");
                $("#" + m).removeClass("rank-a");
                $("#" + m).removeClass("rank-b");
                $("#" + m).removeClass("rank-c");
                $("#" + m).addClass("rank-c")
            }
        }
    }
}
jQuery.validator.addMethod("checkDetailAddress",
function (c, d) {
    return this.optional(d) || /^[0-9a-zA-Z\u3400-\u9FFF\#]+$/.test(c)
},
"wrong name.");
jQuery.validator.addMethod("checkAddressName",
function (c, d) {
    if (/^[-]+$/.test(c)) {
        return false
    }
    return this.optional(d) || /^[a-z A-Z·..\u3400-\u9FFF\-]+$/.test(c) || /^[a-zA-Z·..\u3400-\u9FFF]+$/.test(c)
},
"wrong name.");
jQuery.validator.addMethod("checkAddressSelect",
function (c, d) {
    if ("" == c) {
        return false
    }
    if (c) {
        return true
    }
    return this.optional(d)
},
"wrong name."); (function () {
    $(document).ready(function () {
        $.sidebar_init(7);
        if ("3" != isStudent) {
            $("#_stu_infor").hide()
        }
        $("#passengerType").on("change",
        function () {
            if ($(this).val() == "3") {
                $("#_stu_infor").show(800)
            } else {
                $("#_stu_infor").hide(800)
            }
        });
        $._initForm();
        $._show_id_no();
        $._show_mobile_no();
        initNameNotice()
    });
    var b = new dhtmlXWindows();
    b.enableAutoViewport(true);
    b.setSkin("dhx_terrace");
    b.setImagePath(ctx + "resources/js/rich/windows/imgs/");
    jQuery.extend({
        cancelAdd: function () {
            otsRedirect("post", ctx + "passengers/init")
        },
        _toAdd: function () {
            $("#_addForm").submit()
        },
        _show_id_no: function () {
            $("#cardCode").on("focus",
            function () {
                var e = $("#cardCode").val();
                if (e != "") {
                    var f = "";
                    var a = e.length;
                    if (e.length <= 3) {
                        f = e
                    } else {
                        if (a <= 6) {
                            f = e.substring(0, 3) + " " + e.substring(3, a)
                        } else {
                            if (a <= 10) {
                                f = e.substring(0, 3) + " " + e.substring(3, 6) + "  " + e.substring(6, a)
                            } else {
                                if (a <= 14) {
                                    f = e.substring(0, 3) + " " + e.substring(3, 6) + "  " + e.substring(6, 10) + " " + e.substring(10, a)
                                } else {
                                    f = e.substring(0, 3) + " " + e.substring(3, 6) + "  " + e.substring(6, 10) + " " + e.substring(10, 14) + " " + e.substring(14, a)
                                }
                            }
                        }
                    }
                    $("#passenger_id_no_div").html(f);
                    $("#passenger_id_no_div").css("display", "block")
                } else {
                    $("#passenger_id_no_div").html("");
                    $("#passenger_id_no_div").css("display", "none")
                }
            });
            $("#cardCode").on("keyup",
            function () {
                $("#passenger_id_no_div").css("display", "block");
                var e = $("#cardCode").val();
                var f = "";
                var a = e.length;
                if (e.length <= 3) {
                    f = e
                } else {
                    if (a <= 6) {
                        f = e.substring(0, 3) + " " + e.substring(3, a)
                    } else {
                        if (a <= 10) {
                            f = e.substring(0, 3) + " " + e.substring(3, 6) + "  " + e.substring(6, a)
                        } else {
                            if (a <= 14) {
                                f = e.substring(0, 3) + " " + e.substring(3, 6) + "  " + e.substring(6, 10) + " " + e.substring(10, a)
                            } else {
                                f = e.substring(0, 3) + " " + e.substring(3, 6) + "  " + e.substring(6, 10) + " " + e.substring(10, 14) + " " + e.substring(14, a)
                            }
                        }
                    }
                }
                $("#passenger_id_no_div").html(f)
            });
            $("#cardCode").on("blur",
            function () {
                $("#passenger_id_no_div").html("");
                $("#passenger_id_no_div").css("display", "none")
            })
        },
        _show_mobile_no: function () {
            $("#mobileNo").on("focus",
            function () {
                var e = $("#mobileNo").val();
                if (e != "") {
                    var f = "";
                    var a = e.length;
                    if (e.length <= 3) {
                        f = e
                    } else {
                        if (a <= 7) {
                            f = e.substring(0, 3) + " " + e.substring(3, a)
                        } else {
                            f = e.substring(0, 3) + " " + e.substring(3, 7) + "  " + e.substring(7, a)
                        }
                    }
                    $("#mobile_no_div").html(f);
                    $("#mobile_no_div").css("display", "block")
                } else {
                    $("#mobile_no_div").html("");
                    $("#mobile_no_div").css("display", "none")
                }
            });
            $("#mobileNo").on("keyup",
            function () {
                $("#mobile_no_div").css("display", "block");
                var e = $("#mobileNo").val();
                var f = "";
                var a = e.length;
                if (e.length <= 3) {
                    f = e
                } else {
                    if (a <= 7) {
                        f = e.substring(0, 3) + " " + e.substring(3, a)
                    } else {
                        f = e.substring(0, 3) + " " + e.substring(3, 7) + "  " + e.substring(7, a)
                    }
                }
                $("#mobile_no_div").html(f)
            });
            $("#mobileNo").on("blur",
            function () {
                $("#mobile_no_div").html("");
                $("#mobile_no_div").css("display", "none")
            })
        },
        _initForm: function () {
            $("#_addForm").validate({
                rules: {
                    passenger_name: {
                        required: true,
                        byteRangeLength: [3, 30],
                        checkNameCharBlank: "cardType@cardCode"
                    },
                    sex_code: {
                        required: true
                    },
                    passenger_id_no: {
                        required: true,
                        checkIdValidStr: true,
                        isSecIDCard: "#cardType",
                        isFirIDCard: "#cardType",
                        checkHkongMacao: "#cardType",
                        checkTaiw: "#cardType",
                        checkPassport: "#cardType"
                    },
                    email: {
                        isEmail: true,
                        byteRangeLength: [0, 30]
                    },
                    mobile_no: {
                        isMobile: true
                    },
                    phone_no: {
                        isTelePhone: true
                    },
                    address: {
                        illegalChar: true,
                        byteRangeLength: [0, 200]
                    },
                    postalcode: {
                        isZipCode: true
                    },
                    "studentInfoDTO.school_name": {
                        checkStudentName: "#passengerType"
                    },
                    "studentInfoDTO.department": {
                        studentValidateName: "#passengerType",
                        byteRangeLength: [0, 30]
                    },
                    "studentInfoDTO.school_class": {
                        studentValidateName: "#passengerType",
                        byteRangeLength: [0, 30]
                    },
                    "studentInfoDTO.student_no": {
                        studentRequired: "#passengerType",
                        studentValidateName: "#passengerType",
                        byteRangeLength: [0, 30]
                    },
                    "studentInfoDTO.preference_from_station_name": {
                        checkStudentName: "#passengerType"
                    },
                    "studentInfoDTO.preference_to_station_name": {
                        checkStudentName: "#passengerType"
                    },
                    "studentInfoDTO.preference_card_no": {
                        studentValidateName: "#passengerType",
                        byteRangeLength: [0, 30]
                    },
                    "studentInfoDTO.enter_year": {
                        studentRequired: "#passengerType"
                    }
                },
                messages: {
                    passenger_name: {
                        required: "请输入您的姓名!",
                        byteRangeLength: jQuery.format("允许输入的字符串在{0}-{1}个字符之间!"),
                        checkNameCharBlank: "姓名只能包含中文或者英文,如有生僻字或繁体字参见姓名填写规则进行填写!"
                    },
                    sex_code: {
                        required: "请选择性别"
                    },
                    passenger_id_no: {
                        required: "请输入证件号码!",
                        isSecIDCard: jQuery.format("请正确输入18位的身份证号!"),
                        isFirIDCard: jQuery.format("请正确输入15或者18位的身份证号!"),
                        checkIdValidStr: "输入的证件编号中包含中文信息或特殊字符!",
                        checkHkongMacao: "请输入有效的港澳居民通行证号码!",
                        checkTaiw: "请输入有效的台湾居民通行证号码!",
                        checkPassport: "请输入有效的护照号码!"
                    },
                    email: {
                        isEmail: "请输入有效的电子邮件地址!",
                        byteRangeLength: jQuery.format("允许输入的字符串在{0}-{1}个字符之间!")
                    },
                    mobile_no: {
                        isMobile: "您输入的手机号码不是有效的格式!"
                    },
                    phone_no: {
                        isTelePhone: "您输入的固定电话格式不正确!"
                    },
                    address: {
                        illegalChar: "您输入的地址中含有非法字符!",
                        byteRangeLength: jQuery.format("允许输入的字符串在{0}-{1}个字符之间!")
                    },
                    postalcode: {
                        isZipCode: "您输入的邮编不是有效的格式!"
                    },
                    "studentInfoDTO.school_name": {
                        checkStudentName: "请选择学校名称"
                    },
                    "studentInfoDTO.department": {
                        studentValidateName: "院系只能包含中文、英文、数字!",
                        byteRangeLength: jQuery.format("允许输入的字符串在{0}-{1}个字符之间!")
                    },
                    "studentInfoDTO.school_class": {
                        studentValidateName: "班级只能包含中文、英文、数字!",
                        byteRangeLength: jQuery.format("允许输入的字符串在{0}-{1}个字符之间!")
                    },
                    "studentInfoDTO.student_no": {
                        studentRequired: "请输入学号",
                        studentValidateName: "学号只能包含中文、英文、数字!",
                        byteRangeLength: jQuery.format("允许输入的字符串在1-{1}个字符之间!")
                    },
                    "studentInfoDTO.preference_from_station_name": {
                        checkStudentName: "请输入优惠区间!"
                    },
                    "studentInfoDTO.preference_to_station_name": {
                        checkStudentName: "请输入优惠区间!"
                    },
                    "studentInfoDTO.preference_card_no": {
                        studentValidateName: "优惠卡号只能包含中文、英文、数字!",
                        byteRangeLength: jQuery.format("允许输入的字符串在{0}-{1}个字符之间!")
                    },
                    "studentInfoDTO.enter_year": {
                        studentRequired: "请输入入学年份 "
                    }
                },
                errorPlacement: function (a, d) {
                    if (d.attr("name") == "passenger_name") {
                        a.insertAfter(d.parent().parent());
                        d.parent().parent().next().css("margin-left", d.parent().position().left)
                    } else {
                        if (d.attr("name") == "sex_code") {
                            a.insertAfter(d.parent().parent());
                            d.parent().parent().next().css("margin-left", "130px")
                        } else {
                            if (d.attr("name") == "studentInfoDTO.preference_from_station_name" || d.attr("name") == "studentInfoDTO.preference_to_station_name") {
                                if (d.parent().parent().siblings().length == 8) {
                                    a.insertAfter(d.parent().parent());
                                    d.parent().parent().parent().children("label").css("margin-left", "270px")
                                }
                            } else {
                                a.insertAfter(d.parent());
                                d.parent().next().css("margin-left", "2px").css("line-height", "30px")
                            }
                        }
                    }
                },
                submitHandler: function (i) {
                    var j = null;
                    $("input[name=sex_code]").each(function () {
                        if (this.checked) {
                            j = this.value
                        }
                    });
                    var k = $("#cardType").val();
                    var h = $("#nation").val();
                    if (k == "C" || k == "G" || k == "1" || k == "2") {
                        if (h != "CN") {
                            dhtmlx.alert({
                                title: "输入错误",
                                ok: "确认",
                                text: "请填写正确的国家/地区",
                                type: "alert-error",
                                callback: function () { }
                            });
                            return false
                        }
                    }
                    if (checkSex(j, "#cardType", "#cardCode")) {
                        var l = $("#preference_from_station_name").val();
                        if (l = "简码/汉字") {
                            l = ""
                        }
                        var a = $("#preference_to_station_name").val();
                        if (a = "简码/汉字") {
                            a = ""
                        }
                        $.ajax({
                            url: ctx + "passengers/add",
                            type: "post",
                            data: {
                                passenger_name: $("#name").val(),
                                old_passenger_name: $("#oldName").val(),
                                sex_code: j,
                                _birthDate: null,
                                country_code: $("#nation").val(),
                                passenger_id_type_code: $("#cardType").val(),
                                old_passenger_id_type_code: $("#oldCardType").val(),
                                passenger_id_no: $("#cardCode").val(),
                                old_passenger_id_no: $("#oldCardCode").val(),
                                mobile_no: $("#mobileNo").val(),
                                phone_no: $("#phoneNo").val(),
                                email: $("#email").val(),
                                address: $("#address").val(),
                                postalcode: $("#zipCode").val(),
                                passenger_type: $("#passengerType").val(),
                                "studentInfoDTO.province_code": $("#province_code").val(),
                                "studentInfoDTO.school_code": $("#schoolCode").val(),
                                "studentInfoDTO.school_name": $("#schoolNameText").val(),
                                "studentInfoDTO.department": $("#department").val(),
                                "studentInfoDTO.school_class": $("#school_class").val(),
                                "studentInfoDTO.student_no": $("#student_no").val(),
                                "studentInfoDTO.school_system": $("#school_system").val(),
                                "studentInfoDTO.enter_year": $("#enter_year").val(),
                                "studentInfoDTO.preference_card_no": $("#preference_card_no").val(),
                                "studentInfoDTO.preference_from_station_name": l,
                                "studentInfoDTO.preference_from_station_code": $("#preferenceFromStationCode").val(),
                                "studentInfoDTO.preference_to_station_name": a,
                                "studentInfoDTO.preference_to_station_code": $("#preferenceToStationCode").val()
                            },
                            success: function (c) {
                                if (c.status) {
                                    if (c.data.flag) {
                                        dhtmlx.alert({
                                            title: "添加常用联系人",
                                            ok: "确定",
                                            text: "添加成功!",
                                            type: "dhtmlx_popup_title",
                                            callback: function () {
                                                otsRedirect("post", ctx + "passengers/init")
                                            }
                                        })
                                    } else {
                                        dhtmlx.alert({
                                            title: "添加常用联系人",
                                            ok: "确定",
                                            text: c.data.message,
                                            type: "alert-error"
                                        })
                                    }
                                }
                            }
                        })
                    }
                }
            })
        }
    })
})();
View Code

其中几个基本的验证方法都有了,相信12306的验证方法是很权威的吧。

validateSecIdCard 是验证二代身份证的方法:

技术分享
function validateSecIdCard(n) {
    var d = 0;
    var l = n;
    var i = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙",
        21: "辽宁",
        22: "吉林",
        23: "黑龙",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    if (!/^\d{17}(\d|x)$/i.test(l)) {
        return false
    }
    l = l.replace(/x$/i, "a");
    if (i[parseInt(l.substr(0, 2))] == null) {
        return false
    }
    var j = l.substr(6, 4) + "-" + Number(l.substr(10, 2)) + "-" + Number(l.substr(12, 2));
    var m = new Date(j.replace(/-/g, "/"));
    if (j != (m.getFullYear() + "-" + (m.getMonth() + 1) + "-" + m.getDate())) {
        return false
    }
    for (var k = 17; k >= 0; k--) {
        d += (Math.pow(2, k) % 11) * parseInt(l.charAt(17 - k), 11)
    }
    if (d % 11 != 1) {
        return false
    }
    return true
}
View Code

其他几种证件的验证方法,需要的参照源码提取吧:

技术分享
jQuery.validator.addMethod("checkHkongMacao",
function (f, g, e) {
    if ($(e).val() == "C") {
        var h = /^[HMhm]{1}([0-9]{10}|[0-9]{8})$/;
        return this.optional(g) || (h.test(f))
    } else {
        return true
    }
},
"wrong format.");
jQuery.validator.addMethod("checkTaiw",
function (g, i, j) {
    if ($(j).val() == "G") {
        var f = /^[0-9]{8}$/;
        var h = /^[0-9]{10}$/;
        return this.optional(i) || (f.test(g)) || (h.test(g))
    } else {
        return true
    }
},
"wrong format.");
jQuery.validator.addMethod("checkPassport",
function (f, h, j) {
    if ($(j).val() == "B") {
        var g = /^[a-zA-Z]{5,17}$/;
        var i = /^[a-zA-Z0-9]{5,17}$/;
        return this.optional(h) || (i.test(f)) || g.test(f)
    } else {
        return true
    }
}
View Code

 

简单的分享给大家,没有仔细验证过。

 

12306中前端验证身份证件(及其他证件号)的方法

标签:

原文地址:http://www.cnblogs.com/wxt-home/p/4210652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!