Element UI 自定义正则表达式验证金额

form表单

 <el-form-item label="计划付款金额" prop="plannedAmount">
   <el-input v-model="form.plannedAmount" clearable  placeholder="请输入计划付款金额" />
 </el-form-item>

校验规则

 plannedAmount: [
   {
      required: true, trigger: 'blur', message:'金额不能为空'},
   {
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{
     1,2})?$)|(^(0){
     1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确额格式,可保留两位小数' },
   {
      validator: validateMoney , trigger: 'blur'},
 ],

自定义校验

  data() {
     
     const validateMoney = (rule,value,callback) =>{
     
       console.log('validateMoney',value)
        if(value > this.precontractAmount) {
     
          callback(new Error('计划付款金额需小于合同金额'));
        } else{
     
          callback();
        }
      
    };
    return {
     
      rules: {
     
        plannedAmount: [
          {
      required: true, trigger: 'blur', message:'金额不能为空'},
          {
      pattern: /(^[1-9]([0-9]+)?(\.[0-9]{
     1,2})?$)|(^(0){
     1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确额格式,可保留两位小数' },
          {
      validator: validateMoney , trigger: 'blur'},
        ],

      },
    };
  },
 /** 提交按钮 */
    submitForm: function() {
     
      this.$refs["form"].validate(valid => {
     
        if (valid) {
     
          if (this.form.itemContractPaymentScheduleId != undefined) {
     
            updateSchedule(this.form).then(response => {
     
              if (response.code === 200) {
     
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
     
                this.msgError(response.msg);
              }
            });
          } else {
     
            addSchedule(this.form).then(response => {
     
              if (response.code === 200) {
     
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
     
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },

你可能感兴趣的:(Vue,elementUI,form,校验)