关于项目统一表单验证问题

          <div v-for="(item, index) in setInput" :key="index">
            <p><span class="redDian">*</span>&nbsp;{{item.name}}</p>
            <input :data-typeId="item.typeId" :data-itemModel="item.inputModel" class="formVerify" v-if="item.type == 'number'" v-model="item.inputModel" :placeholder="'请输入' + item.name" :type="item.type" :maxlength="item.maxlength" oninput="if(value.length>11)value=value.slice(0,11)" pattern="[0-9]*" name="" id="">
            <input :data-typeId="item.typeId" :data-itemModel="item.inputModel" class="formVerify" v-if="item.type == 'text' && index != 3" v-model="item.inputModel" :placeholder="'请输入' + item.name" :type="item.type" name="" id="">
            <div :data-typeId="item.typeId" :data-itemModel="questionType" class="formVerify" :class="questionType ? 'c-fc-xblack' : 'c-fc-sgray'" v-if="index == 3" @click="openQuestionType" :type="item.type">{{questionType ? questionType : '请选择' + item.name}}</div>
          </div>
          <div @click="checkbox = !checkbox">
            <span :data-typeId="5" :data-itemModel="checkbox" class="formVerify" :class="checkbox ? 'fcPink' : 'c-fc-gray'">{{checkbox ? '&#xe62b;' : '&#xe741;'}}</span>
            <span>提交即视为已同意<span class="fcPink" @click.stop="showHelp = true;">《个人信息授权与保护声明》</span></span>
            <img @click.stop="getCode" src="@/assets/images/register/but.png" alt="">
          </div>

 

      setInput: [ // 表单循环数据
        { name: '姓名', inputModel: '', type: 'text', maxlength: 20, typeId: 1, },
        { name: '电话', inputModel: '', type: 'number', maxlength: 11, typeId: 2, },
        { name: '公司名称', inputModel: '', type: 'text', maxlength: 20, typeId: 3, },
        { name: '职位名称', inputModel: '', type: 'text', maxlength: 100, typeId: 4, },
      ],



    // 打开验证码提交数据弹窗
    getCode() {
      let tabItem = document.getElementsByClassName('formVerify');
      let msg = '';
      for (var i = 0; i < tabItem.length; i++) {
        msg = this.authenTication(tabItem[i].dataset.itemmodel, tabItem[i].dataset.typeid);
        if (msg) {
          utilJs.showAlert({msg: msg}); // 弹窗
          break;
        }
      }
      if (msg) {
        return;
      }
      this.$nextTick(() => {
        this.showCode = true;
        this.telphone = this.setInput[1].inputModel;
      })
    },




    authenTication(item, type) {
      // 1 姓名 2 电话 3 公司名称 4 职位名称 5 勾选个人协议
      let msg = '';
      let tication = {
        1: () => {
          console.log(item, 'item')
          if (item == '' || item.length > 20) {
            msg = item == '' ? '姓名不能为空' : '姓名输入不能超过20个字'
          }
        },
        2: () => {
          let tel = /^[1][3,4,5,7,8,9][0-9]{9}$/;
          if (tel.test(item) == false || item.length != 11) {
            msg = '请输入正确的手机号'
          }
        },
        3: () => {
          if (item == '' || item > 20) {
            msg = item == '' ? '请输入公司名称' : '公司名称输入不能超过20个字'
          }
        },
        4: () => {
          if (item == '') {
            msg = '请选择职位名称'
          }
        },
        5: () => {
          if (item == false) {
            msg = '请勾选个人授权声明'
          }
        },
      }
      tication[type] && tication[type]();
      return msg;
    },



通过将表单验证提取成公共方法,并且传入参数进行判断是否验证通过

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值