控制输入的内容在规定的区间范围内
<el-form ref="formData" :model="formData" :rules="rules">
<el-form-item label="价格:" prop="price">
<el-input type="text" v-model="formData.priceFrom" placeholder="请输入"></el-input>-
<el-input type="text" v-model="formData.priceTo" placeholder="请输入"></el-input>万
</el-form-item>
</el-form>
data(){
let checkPrice = (rule, value, callback) => {
let priceFrom = this.$refs.formData.model.priceFrom;
let priceTo = this.$refs.formData.model.priceTo;
let reg = /^[1-9]\d*$|^0$/;
if (!priceFrom && !priceTo) {
callback();
} else if (priceFrom && !reg.test(priceFrom)) {
callback(new Error("请输入正整数"));
} else if (priceTo && !reg.test(priceTo)) {
callback(new Error("请输入正整数"));
} else if (priceFrom && priceTo && priceFrom > priceTo) {
callback(new Error("价格区间范围应为0-∞"));
} else {
callback();
}
};
return {
formData: {
priceFrom: "",
priceTo: "",
},
rules: {
price: [{validator: checkPrice,trigger: "blur",},],
}
}
},