步骤一:在src 创建一个文件夹,创建一个文件rules.js
步骤二:在utils/rules.js增加以下代码
// 验证手机号码
export function checkIdNum(rule, value, callback) {
const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
if (!value) {
return callback(new Error('证件号码不能为空'))
} else if (!reg.test(value)) {
return callback(new Error('证件号码不正确'))
} else {
callback()
}
}
export default {
checkId: [{ required: true, validator: checkIdNum, trigger: 'blur' }],
}
步骤三:页面上 import rules from ‘@/utils/rules’
<el-form-item label="手机号码:" prop="phone" :rules="rulesUtils.checkId">
<el-input
v-model="ruleForm.idNum"
placeholder="请输入身份证号码"
clearable>
</el-input>
</el-form-item>
data() {
return {
rulesUtils: rules
}
}