vue 验证公民身份证号 并 自动 获取 性别、年龄、生日
生活随笔
收集整理的這篇文章主要介紹了
vue 验证公民身份证号 并 自动 获取 性别、年龄、生日
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
vue 驗(yàn)證公民身份證號(hào) 并 自動(dòng) 獲取 性別、年齡、生日
第一種方式:
<template><el-form :model="baseInfo" ref="baseForm" :rules="baseFormRules" label-width="110px" class="departmentDetail-ruleForm"><el-form-item label="身份證號(hào):" prop="idCardNo"> <el-input v-model="baseInfo.idCardNo" style="width:60%" /></el-form-item></el-form></template><script>export default {data(){return {baseFormRules: { idCardNo: [{ required: true, message: '身份證號(hào)不能為空', trigger: 'blur' },{ validator: this.checkCard, trigger: 'blur' }],},baseInfo: {},area: {11:"北京",12:"天津",13:"河北",14:"山西",15:"內(nèi)蒙古",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:"臺(tái)灣",81:"香港",82:"澳門",91:"國(guó)外"},Wi: [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 ],ValideCode: [1,0,'X',9,8,7,6,5,4,3,2]}},methods: {// 檢測(cè)身份證checkCard() {if(!this.baseInfo.idCardNo) return;let CardId = this.baseInfo.idCardNo;if(CardId.length == 15) {if(this.is15Card(CardId)) {this.go(CardId.length);}else {return this.$message({type: 'error', message: '您的身份證號(hào)有誤!請(qǐng)輸入你真實(shí)的身份證號(hào),確保你的利益得到保障!'});}} else if (CardId.length == 18) {let a_iden = CardId.split("");if (this.is18Card(CardId) && this.is18CardEnd(a_iden) ) { // && this.is18CardEnd(a_iden)this.go(CardId.length);return this.is18Card(CardId);}else {return this.$message({type: 'error', message: '您的身份證號(hào)有誤!請(qǐng)輸入你真實(shí)的身份證號(hào),確保你的利益得到保障!'});}} else {return this.$message({type: 'error', message: '您的身份證號(hào)有誤!請(qǐng)輸入你真實(shí)的身份證號(hào),確保你的利益得到保障!'});}},// 檢測(cè)18位身份證號(hào)最后一位是否符合要求is18CardEnd(a_idCard) {let sum = 0;if (a_idCard[17].toLowerCase() == 'x') {a_idCard[17] = 10;}for ( var i = 0; i < 17; i++) {sum += this.Wi[i] * a_idCard[i];}let valCodePosition = sum % 11;if (a_idCard[17] == this.ValideCode[valCodePosition]) {return true;} else {return false;}},// 驗(yàn)證最后一位校正碼is18Card(idCard18){let year = idCard18.substring(6,10);let month = idCard18.substring(10,12);let day = idCard18.substring(12,14);let temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));if(temp_date.getFullYear()!=parseFloat(year)||temp_date.getMonth()!=parseFloat(month)-1||temp_date.getDate()!=parseFloat(day)){return false;}else{return true;}},is15Card(idCard15){let year = idCard15.substring(6,8);let month = idCard15.substring(8,10);let day = idCard15.substring(10,12);let temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));if(temp_date.getYear()!=parseFloat(year)||temp_date.getMonth()!=parseFloat(month)-1 ||temp_date.getDate()!=parseFloat(day)) {return false;}else{return true;}},// 實(shí)現(xiàn)自動(dòng)生成生日,性別,年齡go(val) {let iden = this.baseInfo.idCardNo;let sex = null;let birth = null;let myDate = new Date();let month = myDate.getMonth() + 1;let day = myDate.getDate();let age = 0;if(val===18){age = myDate.getFullYear() - iden.substring(6, 10) - 1;sex = iden.substring(16,17);birth = iden.substring(6,10)+"-"+iden.substring(10,12)+"-"+iden.substring(12,14);if (iden.substring(10, 12) < month || iden.substring(10, 12) == month && iden.substring(12, 14) <= day) age++;}if(val===15){age = myDate.getFullYear() - iden.substring(6, 8) - 1901;sex = iden.substring(13,14);birth = "19"+iden.substring(6,8)+"-"+iden.substring(8,10)+"-"+iden.substring(10,12);if (iden.substring(8, 10) < month || iden.substring(8, 10) == month && iden.substring(10, 12) <= day) age++;}if(sex%2 === 0)sex = '女';elsesex = '男';this.baseInfo.sex = sex;this.baseInfo.age = age;this.baseInfo.birthday = birth;this.baseInfo.birthplace = this.area[iden.substring(0,2)];console.log(this.baseInfo)}}} </script><style></style>第二種方式:
<template><el-form :model="baseInfo" ref="baseForm" :rules="baseFormRules" label-width="110px" class="departmentDetail-ruleForm"><el-form-item label="身份證號(hào):" prop="idCardNo"> <el-input v-model="baseInfo.idCardNo" style="width:60%" /></el-form-item></el-form></template><script>export default {data(){return {baseFormRules: { idCardNo: [{ required: true, message: '身份證號(hào)不能為空', trigger: 'blur' },{ validator: this.validID, trigger: 'blur' }],},baseInfo: {},area: {11:"北京",12:"天津",13:"河北",14:"山西",15:"內(nèi)蒙古",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:"臺(tái)灣",81:"香港",82:"澳門",91:"國(guó)外"},Wi: [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 ],ValideCode: [1,0,'X',9,8,7,6,5,4,3,2]}},methods: {// 身份證驗(yàn)證validID(rule,value,callback){// 身份證號(hào)碼為15位或者18位,15位時(shí)全為數(shù)字,18位前17位為數(shù)字,最后一位是校驗(yàn)位,可能為數(shù)字或字符X let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;if (reg.test(value)) {this.go(value.length);callback()} else {callback(new Error('身份證號(hào)碼不正確'))}},// 實(shí)現(xiàn)自動(dòng)生成生日,性別,年齡go(val) {let iden = this.baseInfo.idCardNo;let sex = null;let birth = null;let myDate = new Date();let month = myDate.getMonth() + 1;let day = myDate.getDate();let age = 0;if(val===18){age = myDate.getFullYear() - iden.substring(6, 10) - 1;sex = iden.substring(16,17);birth = iden.substring(6,10)+"-"+iden.substring(10,12)+"-"+iden.substring(12,14);if (iden.substring(10, 12) < month || iden.substring(10, 12) == month && iden.substring(12, 14) <= day) age++;}if(val===15){age = myDate.getFullYear() - iden.substring(6, 8) - 1901;sex = iden.substring(13,14);birth = "19"+iden.substring(6,8)+"-"+iden.substring(8,10)+"-"+iden.substring(10,12);if (iden.substring(8, 10) < month || iden.substring(8, 10) == month && iden.substring(10, 12) <= day) age++;}if(sex%2 === 0)sex = '女';elsesex = '男';this.baseInfo.sex = sex;this.baseInfo.age = age;this.baseInfo.birthday = birth;this.baseInfo.birthplace = this.area[iden.substring(0,2)];console.log(this.baseInfo)}}} </script><style></style>總結(jié)
以上是生活随笔為你收集整理的vue 验证公民身份证号 并 自动 获取 性别、年龄、生日的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ERP系统-库存子系统-申购单
- 下一篇: Arcgis经纬线标注设置(英文、刻度显