首页 > 代码库 > 根据身份证获取出生日期及性别

根据身份证获取出生日期及性别

CreateTime--2017年6月2日11:45:16
Author:Marydon

根据身份证获取出生日期及性别

  第一步:身份证号格式校验

/** * 身份证号格式校验 */function idValidator(obj) {    var provinces={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:"国外"           };    var expression=/(^\d{15}$)|(^\d{17}(\d|X)$)/;    var isViald=expression.test(obj.value);    if (isViald)    {       isViald=provinces[obj.value.substr(0,2)]?true:false;    }    return isViald;}

  第二步:通过身份证获取出生日期及性别

/**   * 通过身份证获取出生日期及性别 * @param idCard 15/18位身份证号码    * @return JSON对象  *         sex:0-女、1-男; *         birthDay:yyyy-MM-dd */function getBirthdayAndSex(idCard) {    var info = {};    var birth = (idCard.length === 18) ? idCard.slice(6, 14) : idCard.slice(6, 12);    // 18位:提取第17位数字;15位:提取最后一位数字    var order = (idCard.length == 18) ? idCard.slice(-2,-1):idCard.slice(-1);    info.birthDay = (idCard.length === 18) ? ([birth.slice(0, 4),            birth.slice(4, 6), birth.slice(-2)            ]).join(‘-‘) : ([                ‘19‘ + birth.slice(0, 2), birth.slice(2, 4),                birth.slice(-2)            ]).join(‘-‘);    // 余数为0代表女性,不为0代表男性            info.sex = (order % 2 === 0 ? 0 : 1);    return info;        }

  第三步:改变出生日期及性别的值及只读状态

/** * 改变出生日期及性别的值及只读状态 */function changeCardInfo(obj) {        if ("" == obj.value && !document.getElementById(‘BIRTH_DAY2‘).disabled)        return;        // 格式校验结果    var result = idValidator(obj);    if (result) {        // 获取出生日期及性别        var cardInfo = getBirthdayAndSex(obj.value);        var birthDay = cardInfo.birthDay;        var sex = cardInfo.sex;                document.getElementById(‘BIRTH_DAY2‘).disabled = true;        document.getElementById(‘BIRTH_DAY2‘).value =http://www.mamicode.com/ birthDay;        document.getElementById(‘BIRTH_DAY‘).value =http://www.mamicode.com/ birthDay;                document.getElementById(‘male‘).disabled = true;        document.getElementById(‘female‘).disabled = true;                if (1 == sex) {            document.getElementById(‘male‘).checked = true;            document.getElementById(‘SEX‘).value = http://www.mamicode.com/‘男‘;        } else {            document.getElementById(‘female‘).checked = true;                document.getElementById(‘SEX‘).value = http://www.mamicode.com/‘女‘;        }    } else {//重置        document.getElementById(‘BIRTH_DAY‘).valuehttp://www.mamicode.com/= "";        document.getElementById(‘BIRTH_DAY2‘).valuehttp://www.mamicode.com/= "";        document.getElementById(‘BIRTH_DAY2‘).disabled = false;        document.getElementById(‘male‘).disabled = false;        document.getElementById(‘female‘).disabled = false;        document.getElementById(‘male‘).checked = true;        document.getElementById(‘SEX‘).value = http://www.mamicode.com/‘男‘;    }    }

  HTML

<table border="0" cellpadding="1" cellspacing="0" align="center">    <tbody>        <tr>            <td>身份证号</td>            <td>                <input name="CARD_ID" class="TextBox" type="text" dataType="idcard" id="CARD_ID" errMessage="请输入正确的身份证号!"                    onblur="changeCardInfo(this);" />            </td>            <td>性别</td>            <td>                <label>                    <input name="SEX1" type="radio" checked id="male" onchange="$(‘#SEX‘).val(‘男‘);" /></label>                <label>                    <input name="SEX1" type="radio" id="female" onchange="$(‘#SEX‘).val(‘女‘);" /></label>                <input name="SEX" type="hidden" id="SEX" value="男" />            </td>            <td>出生日期</td>            <td>                <input class="TextBox" readonly id="BIRTH_DAY2" type="text" onclick="WdatePicker({maxDate:‘%y-%M-%d‘});"                    onblur="$(‘#BIRTH_DAY‘).val(this.value)" />                <input name="BIRTH_DAY" id="BIRTH_DAY" type="hidden" />            </td>        </tr>    </tbody></table>

 

 

根据身份证获取出生日期及性别