前言:vue里写表单校验的时候,必填项输入全是空格的时候,校验依旧会通过,但是这不是我们所期望的,我们期望输入全是空格的时候校验必填项时依旧不通过,那么我们就需要对值进行处理,去除其中的空格。很庆幸的是,vue有一个很简单编辑的方法,直接去空格,不用每一个都去写校验逻辑,下面就是针对以上问题,写的方法。

去空格之前的form表单

<div class="form-box">
      <el-form
        ref="formData"
        :model="detailData"
        :rules="rules"
        label-width="100px"
        class="card-ruleForm"
      >
        <el-form-item
          label="TEL"
          prop="telephone"
        >
          <el-col :span="6">
            <el-input
              v-model="detailData.telephone"
              placeholder="请输入"
            />
          </el-col>
        </el-form-item>
        <el-form-item
          label="MAL"
          prop="email"
        >
          <el-col :span="6">
            <el-input
              v-model="detailData.email"
              placeholder="请输入"
            />
          </el-col>
        </el-form-item>
        <el-form-item class="last-el-form-item">
          <el-button @click="handleCancle">取消</el-button>
          <el-button :loading="loading" type="primary" @click="onSubmit('formData')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>

去空格之后的form表单

<div class="form-box">
      <el-form
        ref="formData"
        :model="detailData"
        :rules="rules"
        label-width="100px"
        class="card-ruleForm"
      >
        <el-form-item
          label="TEL"
          prop="telephone"
        >
          <el-col :span="6">
            <el-input
              v-model.trim="detailData.telephone"
              placeholder="请输入"
            />
          </el-col>
        </el-form-item>
        <el-form-item
          label="MAL"
          prop="email"
        >
          <el-col :span="6">
            <el-input
              v-model.trim="detailData.email"
              placeholder="请输入"
            />
          </el-col>
        </el-form-item>
        <el-form-item class="last-el-form-item">
          <el-button @click="handleCancle">取消</el-button>
          <el-button :loading="loading" type="primary" @click="onSubmit('formData')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>

解决方法:

将v-model改为v-model.trim即可

Logo

前往低代码交流专区

更多推荐