数据格式
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)
// 18
if (/^\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 = 0
for (let i = 0; i < 17; i++) sum += wi[i] * id[i]
if (y[sum % 11] === id.pop().toLowerCase()) info.valid = true
return this.back(info, backInfo)
} else if (/^\d{15}$/.test(id)) { // 15
if (this.initDate(15, info, id)) info.valid = true
return this.back(info, backInfo)
} else {
return this.back(info, backInfo)
}
},
back (info, backInfo) {
return backInfo ? info : info.valid
},
initDate (length, info, id) {
info.length = length
let a
a = (length === 15) ? 0 : 2 // 15:18
let temp
info.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 + '-' + borthNumD
if (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 + '-' + borthNumD
if (SexNum % 2 === 0) {
this.dataobj.gender = '1'
} else {
this.dataobj.gender = '0'
}
}
} else {
console.log('请输入正确的身份证号')
}
},