el-form 表单填写验证

<el-form ref="formData" :model="formData" label-width="100px" :rules="rules">
   <el-form-item label="客户名称" prop="name">
     <el-input v-model="formData.name" autocomplete="off" />
   </el-form-item>
   <el-form-item label="截止时间" prop="time">
     <el-date-picker
       v-model="formData.time"
       type="datetime"
       placeholder="选择日期时间"
       :picker-options="pickerOptions"
     ></el-date-picker>
   </el-form-item>
   <el-form-item label="任务积分" prop="taskIntegral">
     <el-input v-model="formData.taskIntegral" autocomplete="off" />
   </el-form-item>
 </el-form>
data() {
    //只允许中英文 + 数字 + 允许的标点符号
    const specialCharacter = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入客户名称"));
      } else {
        if (/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\.\-\——\(\)\+\$]/g.test(value)) {
          callback(new Error("请不要包含空格及特殊字符"));
        } else {
          callback();
        }
      }
    };

    //正整数和0  含0(/^([1-9]\d*|[0]{1,1})$/)
    const validNumber = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入任务积分"));
      } else {
        if (!/^[1-9]+\d*$/.test(value)) {
          callback(new Error("请输入正整数"));
        } else {
          callback();
        }
      }
    };
    return {
      rules: {
        name: [
          { required: true, trigger: "blur", validator: specialCharacter }
        ],
        time: [{ required: true, message: "请选择截止时间", trigger: "blur" }],
        taskIntegral: [
          { required: true, trigger: "blur", validator: validNumber }
        ]
      },
      formData: {
        name: "",
        time: "",
        taskIntegral: ""
      },
    };
  },
  methods:{
  confirmFn(flag) {
      let params = {
        operationType: "addColdCallTask",
        customer_name: this.formData.name,
        dead_line: this.$moment(this.formData.time).format(
          "YYYY-MM-DD HH:mm:ss"
        ),
        score: this.formData.taskIntegral
      };
        this.$refs.formData.validate(async valid => {
          if (valid) {
            console.log("submit!");
          } else {
            return false;
          }
        });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值