转载自大佬:vue 根据输入的身份号码,自动获取年龄 - 心意如水hucuie22 - 博客园
getName(iden) {
let val = iden.length;
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;
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;
if (iden.substring(8, 10) < month || iden.substring(8, 10) == month && iden.substring(10, 12) <= day) age++;
}
this.age = age;
},
<li>
<form label="身份证号" prop="idCard">
<span>身份证号:</span>
<input v-model="idCard" placeholder="请输入身份证号" @blur="getName(idCard)" class="input" tyle="text"></input>
<div style="clear:both;"></div>
</form>
</li>
<li>
<form label="年龄" prop="age">
<span>年龄:</span>
<input v-model="age" placeholder="年龄" class="input"></input>
<div style="clear:both;"></div>
</form>
</li>