当前位置: 首页 > news >正文

写一个输入框校验类,链式实现表单校验

// 数据校验类
export class ValidateClass {
  constructor (input, label) {
    this._input = input
    this._label = label
    this.errorMessage = '通过校验' // 错误信息
    this.pass = true // 校验是否通过
  }

  // 数据输入
  data (input) {
    if (!this.pass) return this
    this._input = input
    return this
  }

  // 标识符输入
  key (key) {
    if (!this.pass) return this
    this._key = key
    return this
  }

  // 标题输入
  label (label) {
    if (!this.pass) return this
    this._label = label
    return this
  }

  // 必填,不能为空
  isRequired (message) {
    if (!this.pass) return this
    if (
      /^\s*$/g.test(this._input) ||
      this._input === null ||
      this._input === undefined
    ) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }

  // 允许最小数值
  minNumber (min, message) {
    if (!this.pass) return this
    if (+(this._input) < min) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }

  // 最小长度
  minLength (length, message) {
    if (!this.pass) return this
    if (this._input.length < length) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }

  // 允许最大数值
  maxNumber (max, message) {
    if (!this.pass) return this
    if (+(this._input) > max) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }

  // 最大长度
  maxLength (length, message) {
    if (!this.pass) return this
    if (this._input.length > length) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }

  // 手机号校验
  isPhone (message) {
    if (!this.pass) return this
    const phoneReg = /^1[3|4|5|7|8|9][0-9]\d{8}$/
    if (!phoneReg.test(this._input)) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }

  // 自定义正则校验
  requireRegexp (reg, message) {
    if (!this.pass) return this
    if (!reg.test(this._input)) {
      this.errorMessage = this._label ? this._label + message : message
      this.pass = false
    }
    return this
  }
}

使用时

<div class="input-box">
  <van-field class="my-input" v-model="inputVal" type="number" :placeholder="placeholder" :input-align="inputAlign" :maxlength="maxlength" :readonly="readonly" @blur="handleValidate" @keyup.native="inputVal = inputVal.replace(/[^\.\d]/g, '')" />
  <van-icon v-if="inputVal" name="clear" @click="clearValue" :class="unit ? '' : 'computed-position'" />
</div>
<p v-if="errorTip" class="error-tip" :style="{'margin-bottom': isMultiple ? '10px' : ''}">{{ errorTip }}</p>
handleValidate () {
  const reg = /^0+$/g // 以0开头多个0并且以0结尾
  if (!reg.test(this.inputVal)) {
    this.inputVal = this.inputVal.replace(/^0+/g, '') // 如果两位以上的数字第一位是0,则替换成''
  } else {
    this.inputVal = this.inputVal.replace(/^0+/g, '0')
  }
  if (this.inputVal) {
    this.errorTip = ''
    this.inputVal = this.numberCheck(this.inputVal) // 小数点后只允许输入一位
    const cValidate = new ValidateClass(+this.inputVal) // 引入校验类
    cValidate
      .minNumber(this.range[0], this.minTip)
      .maxNumber(this.range[1], this.maxTip)
    if (!cValidate.pass) {
      this.errorTip = cValidate.errorMessage // 错误提示
    } else {
      this.errorTip = ''
    }
  } else {
    this.errorTip = this.requireTip
  }
},

相关文章:

  • sql2022 复制 事务级别发布后无法删除
  • 在IDEA中使用TortoiseSVN
  • 自然语言处理(NLP)技术的应用面有哪些
  • 往期项目shader着色器实践效果应用合集
  • 【网络通信安全】基于华为 eNSP 的链路聚合、手工负载分担模式与 LACP 扩展配置 全解析
  • 【时时三省】(C语言基础)if语句一般形式
  • 上下文离群值深度解析:定义、应用场景与检测方法实战
  • 第 12 章(番外)| Solidity 安全前沿趋势 × 审计生态 × 职业路径规划
  • 一周掌握Flutter开发--8. 调试与性能优化(上)
  • 高效事件驱动设计模式——Reactor 模式
  • 【点盾云】加密技术如何防止视频内容随意传播?
  • Angular的理解
  • HTTP 1.0和2.0 有什么区别?
  • 超详细docker部署搭建私有仓库harbor
  • 【django】2-1 (django配置) 应用配置、中间件配置、模板配置
  • 食品计算—Augmented/mixed reality technologies for food: A review
  • Linux 常见漏洞修复方法和防御措施整理
  • 【git】基本操作
  • fork系统调用
  • 总结ExecutorService和CountDownLatch的作用、用法和场景
  • “行人相撞案”现场视频公布,法院:表述不当造成误导
  • 碧桂园:砸锅卖铁保交房、持续推进保主体,尽快让公司恢复正常经营
  • 网络主播直播泄机密,别让这些“小事”成威胁国家安全的“突破口”
  • 上海“世行对标改革”的税务样本:设立全国首个税务审判庭、制定首个税务行政复议简易程序
  • 习近平向中国人民解放军仪仗队致意
  • 赵作海因病离世,妻子李素兰希望过平静生活