vue中elementUI价格校验及正则
<el-formref="add"
:rules="rules"
:model="add"
label-width="110px"
>
<el-form-item label="价格:" v-if="add.serviceType!=3" prop="amount">
<input type="text" style="width: 50%" autocomplete="off" placeholder="请输入" v-model="add.amount" class="el-input__inner m_r_20">元
</el-form-item>
</el-form>
rules: {amount: [
{required: true, message: "请输入", trigger: "blur,change"},
{ min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur,change' },
{ validator: twoPoint, message: '最多包含两位小数的正数且不能为以0开头的正整数', trigger: 'blur' }
],
},
const twoPoint = (rule, value, callback) => {
if(/^\d+\.?\d{0,2}$/.test(value)){
if(value.indexOf('.')=='-1'&&value.length>1&&value.slice(0,1)=='0'){
callback(new Error("最多包含两位小数的正数且不能为以0开头的正整数"));
}
}else{
callback(new Error("最多包含两位小数的正数且不能为以0开头的正整数"));
}
// if (/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/.test(value)) {
// callback()
// } else {
// callback(new Error('最多包含两位小数的正数'))
// }
}
以上是 vue中elementUI价格校验及正则 的全部内容, 来源链接: utcz.com/z/380626.html