vue 实现v-model自动校验手机号
<template>
<el-input
v-model="mobile"
:placeholder="placeholder"
@change="validate"
maxlength="11"
show-word-limit
></el-input>
</template>
<script>
let mobileReg = /^1[3-9]\d{9}$|^([6|9])\d{7}$|^[0][9]\d{8}$|^[6]([8|6])\d{5}$/;
export default {
model: {
prop: "value",
event: "change",
},
props: ["value", "placeholder"],
data() {
return {
mobile: "",
};
},
watch: {
value() {
this.mobile = this.value;
},
},
methods: {
validate() {
let v = this.mobile;
if (!mobileReg.test(v) && v) {
this.mobile = "";
this.$message.error("请输入正确的手机号");
this.$emit("input", "");
}
this.$emit("change", this.mobile);
},
},
};
</script>