zoukankan      html  css  js  c++  java
  • 策略模式

    class Validators {
      constructor () {
        this.validators = []
        this.strategies = {}
      }
    
      // 增加一个输入框验证器
      addValidator (rule, errMsg, value, ...rest) {
        let ruleElements = rule.split(':')
    
        this.validators.push(() => {
          let strategy = ruleElements.shift()
          let params = ruleElements
          params.unshift(value)
          params.unshift(errMsg)
          return this.strategies[strategy].apply(this, params.concat(rest))
        })
      }
    
      // 返回当前输入框验证器 产生的错误消息
      getInvalidMsg (errMsg, value) {
        return {
          'value': value,
          'errMsg': errMsg
        }
      }
    
      // 逐个验证器校验
      check () {
        let validator = null
        let len = this.validators.length
        for (let i = 0; i < len; i++) {
          validator = this.validators[i]
          let result = validator()
          if (result) {
            return result
          }
        }
      }
    
      // 添加策略算法
      addValidationStrategy (name, strategy) {
        this.strategies[name] = strategy
      }
    
      importStrategies (strategies) {
        for (let strategyName in strategies) {
          this.addValidationStrategy(strategyName, strategies[strategyName])
        }
      }
    }
    
    const validationStrategies = {
      isAccount (errMsg, value) {
        if (/^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/.test(value)) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
    
      isNick (errMsg, value) {
        // 汉字英文数字区分大小写下划线
        if (!/^[u4e00-u9fa5a-zA-Z0-9_]+$/.test(value)) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
      isNoEmpty (errMsg, value) {
        if (value === '') {
          return this.getInvalidMsg(errMsg, value)
        }
      },
      isZhCN (errMsg, value) {
        if (!(/^[u4e00-u9fa5]*$/).test(value)) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
      minLength (errMsg, value, length) {
        if (value.length < length) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
    
      maxLength (errMsg, value, length) {
        if (value.length > length) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
      isMobile (errMsg, value) {
        if (!/^1(d){10}$/.test(value)) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
      isMail (errMsg, value) {
        let reg = /^(w-*.*)+@(w-?)+(.w{2,})+$/
        if (!reg.test(value)) {
          return this.getInvalidMsg(errMsg, value)
        }
      },
      isEqual (errMsg, value, appoint) {
        if (value !== appoint) {
          return this.getInvalidMsg(errMsg, value)
        }
      }
    }
    
    export { Validators, validationStrategies }
    
    
    //使用
     validateParams (params) {
          var validators = new Validators()
    
          validators.importStrategies(validationStrategies)
          validators.addValidator('isNoEmpty', '请填写昵称', params.nickname)
    
          if (this.__hasMutiIdentity) {
            validators.addValidator('isNick', '请输入正确昵称', params.nickname)
            validators.addValidator('minLength:6', '昵称长度位6~12位中文', params.nickname)
            validators.addValidator('maxLength:12', '昵称长度位6~12位中文', params.nickname)
            validators.addValidator('isAccount', '账号必须包含英文和数字', params.nickname)
            validators.addValidator('minLength:6', '账号长度位6~12位中文', params.account)
            validators.addValidator('maxLength:12', '账号长度位6~12位中文', params.account)
          } else {
            validators.addValidator('isZhCN', '昵称必须是中文', params.nickname)
            validators.addValidator('minLength:2', '昵称长度位2~6位中文', params.nickname)
            validators.addValidator('maxLength:6', '昵称长度位2~6位中文', params.nickname)
            validators.addValidator('isNoEmpty', '请填写手机号', params.phone)
            validators.addValidator('isMobile', '请填写正确的手机号', params.phone)
            validators.addValidator('isNoEmpty', '请填写验证码', params.captcha)
            validators.addValidator('isEqual', '请输入正确的验证码', params.captcha.length, 6)
          }
    
          validators.addValidator('isNoEmpty', '请填写密码', params.password)
          validators.addValidator('isNoEmpty', '请确认密码', params.confirmPassword)
          validators.addValidator('isEqual', '两次密码输入不一致', params.password, params.confirmPassword)
    
          var result = validators.check()
    
          if (result) {
            this.showErrorTip(result.errMsg)
            return false
          } else {
            return true
          }
        }
    

      参考: https://www.cnblogs.com/kongxianghai/p/4985122.html

  • 相关阅读:
    输入框联想
    SyntaxError: missing ; before statement 错误的解决
    Oracle数据库DECODE函数的使用.
    MySQL ----命令总结!
    个介!
    递归函数
    闭包函数与装饰器
    函数对象
    力扣题
    函数基础
  • 原文地址:https://www.cnblogs.com/dhsz/p/8134508.html
Copyright © 2011-2022 走看看