74 lines
2.2 KiB
Plaintext
74 lines
2.2 KiB
Plaintext
<template>
|
|
<div class="aa-idcard">
|
|
<div class="aa-idcard-field" :class="{ 'is-focus': focused, 'is-valid': valid, 'is-invalid': invalid }">
|
|
<input
|
|
class="aa-idcard-input"
|
|
:value="value"
|
|
maxlength="18"
|
|
:placeholder="placeholder"
|
|
@input="onInput"
|
|
@focus="focused = true"
|
|
@blur="focused = false"
|
|
/>
|
|
</div>
|
|
<div v-if="result.birth" class="aa-idcard-info">
|
|
<span>出生日期:<b>{{ result.birth }}</b></span>
|
|
<span>性别:<b>{{ result.gender }}</b></span>
|
|
</div>
|
|
<div class="aa-idcard-tip" :class="tipClass">{{ tipText }}</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
var WEIGHTS = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
|
var CHECK = '10X98765432';
|
|
function validate(v) {
|
|
if (!/^\d{17}[\dX]$/.test(v)) return null;
|
|
var sum = 0;
|
|
for (var i = 0; i < 17; i++) sum += parseInt(v[i], 10) * WEIGHTS[i];
|
|
if (CHECK[sum % 11] !== v[17]) return null;
|
|
var birth = v.slice(6, 14);
|
|
return { birth: birth.slice(0, 4) + '-' + birth.slice(4, 6) + '-' + birth.slice(6, 8), gender: parseInt(v[16], 10) % 2 === 1 ? '男' : '女' };
|
|
}
|
|
|
|
export default {
|
|
name: 'AaIDCardInput',
|
|
props: {
|
|
value: { type: String, default: '' },
|
|
placeholder: { type: String, default: '请输入 18 位身份证号' }
|
|
},
|
|
data() {
|
|
return { focused: false };
|
|
},
|
|
computed: {
|
|
result() {
|
|
return validate(this.value.toUpperCase()) || {};
|
|
},
|
|
valid() {
|
|
return !!this.result.birth;
|
|
},
|
|
invalid() {
|
|
return this.value.length === 18 && !this.result.birth;
|
|
},
|
|
tipClass() {
|
|
if (!this.value) return '';
|
|
return this.valid ? 'valid' : this.invalid ? 'invalid' : '';
|
|
},
|
|
tipText() {
|
|
if (!this.value) return '示例:11010119900307657X';
|
|
if (this.value.length < 18) return '已输入 ' + this.value.length + ' 位,继续…';
|
|
return this.valid ? '校验通过' : '身份证号校验失败,请检查';
|
|
}
|
|
},
|
|
methods: {
|
|
onInput(e) {
|
|
var v = e.target.value.replace(/[^0-9Xx]/g, '').toUpperCase().slice(0, 18);
|
|
e.target.value = v;
|
|
this.$emit('input', v);
|
|
}
|
|
}
|
|
};
|
|
</script>
|
|
|
|
<style src="./IDCardInput.css"></style>
|