vue表單:輸入身份證號碼則自動獲取對應的年齡和性別,,若不輸入身份證號則自己填寫年齡和性別


<el-input
:ref="item.meta.system_id"
v-model="temp[item.meta.valueKey]"
clearable
oninput="if(value.length>18)value=value.slice(0,18)"
:disabled="item.meta.disabled"
:placeholder="item.meta.placeholder ? item.meta.placeholder : '請輸入'"
@blur="onBlurCard"
/>

// 電話輸入框失去焦點即表示該輸入框輸入了值且通過正則表達式是個合法是身份證號碼
onBlurCard() {

const val = this.temp['idCardNo'].length // 獲取電話號碼長度
const iden = this.temp['idCardNo'] // 獲取電話號碼輸入框的值
let sex = null
const myDate = new Date()
const month = myDate.getMonth() + 1
const day = myDate.getDate()
let age = 0

if (val === 18) {
age = myDate.getFullYear() - iden.substring(6, 10) - 1
sex = iden.substring(16, 17)
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)
if (iden.substring(8, 10) < month || iden.substring(8, 10) === month && iden.substring(10, 12) <= day) age++
}

if (sex % 2 === 0) { sex = 2 } else { sex = 1 } // sex的值需要根據情況而定,,這里協議的值為:
studentSex: new Map([ // 學生性別
[2, '女'],
[1, '男'],
[3, '未知']
]),

this.temp['studentSex'] = sex // 設置表單中性別的值
this.temp['studentAge'] = age // 設置表單中年齡字段的值
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM