vue 身份证验证
?數據格式
data () {return {show: false,type: 'add',saveLoading: false,dataobj: {name: '', // 姓名idType: '', // 證件類型id: '', // 證件號birthday: '', // 出生日期gender: '', // 性別relations: '', // 關系insuredAmount: '', // 保額paymentMethod: '', // 交費方式期限premium: '' // 保費}}},?
validateIdCard (id, backInfo) {let info = {y: '1900',m: '01',d: '01',sex: 'male',valid: false,length: 0}if (typeof id !== 'string') return this.back(info, backInfo)// 18if (/^\d{17}[0-9x]$/i.test(id)) {if (!this.initDate(18, info, id)) return this.back(info, backInfo)id = id.toLowerCase().split('')let wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]let y = '10x98765432'.split('')let sum = 0for (let i = 0; i < 17; i++) sum += wi[i] * id[i]if (y[sum % 11] === id.pop().toLowerCase()) info.valid = truereturn this.back(info, backInfo)} else if (/^\d{15}$/.test(id)) { // 15if (this.initDate(15, info, id)) info.valid = truereturn this.back(info, backInfo)} else {return this.back(info, backInfo)}},back (info, backInfo) {return backInfo ? info : info.valid},initDate (length, info, id) {info.length = lengthlet aa = (length === 15) ? 0 : 2 // 15:18let tempinfo.y = (a ? '' : '19') + id.substring(6, 8 + a)info.m = id.substring(8 + a, 10 + a)info.d = id.substring(10 + a, 12 + a)info.sex = id.substring(14, 15 + a) % 2 === 0 ? 'female' : 'male'temp = new Date(info.y, info.m - 1, info.d)return (temp.getFullYear() === info.y * 1) &&(temp.getMonth() + 1 === info.m * 1) &&(temp.getDate() === info.d * 1)},身份證失去光標事件判斷身份證號是否正確
IdCardBlur () {if (this.validateIdCard(this.dataobj.id)) {if (this.dataobj.id.length === 18) {let borthNumY = this.dataobj.id.substr(6, 4)let borthNumM = this.dataobj.id.substr(10, 2)let borthNumD = this.dataobj.id.substr(12, 2)let SexNum = this.dataobj.id.substr(16, 1)this.dataobj.birthday = borthNumY + '-' + borthNumM + '-' + borthNumDif (SexNum % 2 === 0) {this.dataobj.gender = '1'} else {this.dataobj.gender = '0'}} else if (this.dataobj.id.length === 15) {let borthNumY = this.dataobj.id.substr(6, 2)let borthNumM = this.dataobj.id.substr(8, 2)let borthNumD = this.dataobj.id.substr(10, 2)let SexNum = this.dataobj.id.substr(14, 1)this.dataobj.birthday = '19' + borthNumY + '-' + borthNumM + '-' + borthNumDif (SexNum % 2 === 0) {this.dataobj.gender = '1'} else {this.dataobj.gender = '0'}}} else {console.log('請輸入正確的身份證號')}},?
總結
- 上一篇: MySQL8.0版本和5.7通过Navi
- 下一篇: 机器学习之线性代数总结