element 表单规则验证

1.输入框整数保留9位小数点保留6位

 <el-form-item label="最大重量(kg)" prop="whPMaxWeight">
     <el-input v-model.number="batchDetail.whPMaxWeight" type="number"
           style="width: 100%" show-word-limit clearable
           placeholder="请输入" />
   </el-form-item>
   
  const validatePass = (rule, value, callback) => {
    var reg = /^(\d{0,9})(\.(\d{0,6}))?$/g;
    if(!reg.test(value)){
      callback(new Error("请输入整数9位小数6位的数值"));
    } else {
      callback();
    }
  };
   const rules = {
    whPMaxWeight:[{ validator:validatePass, trigger: "blur"}],
  };

2.小数点不能超过两位并是正数

 const low = (rule, value, callback, source, options) => {
   if (
    value !== "" &&
    !(/^\d+(\.\d{1,2})?$/.test(value) || /^\d$/.test(value))
   ) {
    callback(new Error("小数点不能超过两位并是正数"));
   } else {
    callback();
   }
  };

3.只能输入字母

  var validateText = (rule, value, callback) => {
   if (value !== "" && /[^a-zA-Z]/g.test(value)) {
    callback(new Error("请输入字母"));
   } else {
    callback();
   }
  };
  oninput="value=value.replace(/[^0-9]/g,'');if(String(value)==='0'){value=1}"

你可能感兴趣的:(element,javascript,前端,vue.js)