Element-UI 中使用rules验证 金额 数字
data中定义验证规则:
var checkCount = function (rule, val, callback) { if (!val) { return callback(new Error("数量不能为空")); } var reg = /^(?!(0[0-9]{0,}$))[0-9]{1,}(\.{0,1}\d{1,3}){0,1}$/; if (!reg.test(val)) { return callback(new Error("格式错误,至多三位小数")); } return callback(); }; var checkPrice = function (rule, val, callback) { if (!val) { return callback(new Error("单价不能为空")); } var reg = /^\d+(\.{0,1}\d{1,4}){0,1}$/; if (!reg.test(val)) { return callback(new Error("格式错误,至多四位小数")); } return callback(); }; var checkScore = function (rule, val, callback) { if (!val) { return callback(new Error("分值不能为空")); } var reg = /^\d+(\.{0,1}\d{1,2}){0,1}$/; if (!reg.test(val)) { return callback(new Error("格式错误,至多两位小数")); } if(parseFloat(val) > 100){ return callback(new Error("分值最大只能是100")); } return callback(); };
rules 下面进行引用data中的验证规则
rules: { deptName: [{ required: true, message: "请选择", trigger: ["change", "blur"] }], score: [{ required: true, message: " ", trigger: ["change", "blur"] }, { validator: checkScore, trigger: ["change", "blur"] }], count: [{ required: true, message: " ", trigger: ["change", "blur"] }, { validator: checkCount, trigger: ["change", "blur"] }], price: [{ required: true, message: " ", trigger: ["change", "blur"] }, { validator: checkPrice, trigger: ["change", "blur"] }], targetName: [{ required: true, message: " ", trigger: ["change", "blur"] }], }
模板中使用:
<el-form-item label="报送结算金额(含税):" prop="totalSettleAmount" :rules="rules.price">
<el-input v-model="value.totalSettleAmount" placeholder="请输入报送结算金额(含税)" clearable ><template slot="append">元</template></el-input>
</el-form-item>