最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一百行JS代碼實現(xiàn)一個校驗工具

 更新時間:2019年04月30日 10:32:53   作者:陳小俊  
這篇文章主要介紹了一百行JS代碼實現(xiàn)一個校驗工具,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

做過校驗需求的小伙伴們都知道,校驗其實是個麻煩事。

規(guī)則多,需要校驗的字段多,都給我們前端帶來巨大的工作量。

一個不小心,代碼里就出現(xiàn)了不少if else等不可維護的代碼。

因此,我覺得一個團隊或者是一個項目,需要一個校驗工具,簡化我們的工作。

首先,參考一下 Joi。只看這一小段代碼:

Joi.string().alphanum().min(3).max(30).required()

我希望我的校驗工具Coi也是鏈式調(diào)用,鏈式調(diào)用可以極大的簡化代碼。

校驗呢,其實主要就3個入?yún)ⅲ盒枰r灥臄?shù)據(jù),提示的錯誤信息,校驗規(guī)則。

哎 直接把代碼貼出來吧,反正就一百行,一目了然:

export default class Coi {
  constructor(prop) {
    this.input = prop
    this.errorMessage = '通過校驗' // 錯誤信息
    this.pass = true // 校驗是否通過
  }
  // 數(shù)據(jù)輸入
  data(input) {
    if (!this.pass) return this
    this.input = input
    return this
  }
  // 必填,不能為空
  isRequired(message) {
    if (!this.pass) return this
    if (
      /^\s*$/g.test(this.input) ||
      this.input === null ||
      this.input === undefined
    ) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
  // 最小長度
  minLength(length, message) {
    if (!this.pass) return this
    if (this.input.length < length) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
  // 最大長度
  maxLength(length, message) {
    if (!this.pass) return this
    if (this.input.length > length) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
  // 需要的格式 number: 數(shù)字, letter: 字母, chinese: 中文
  requireFormat(formatArray, message) {
    if (!this.pass) return this
    let formatMap = {
      number: 0,
      letter: 0,
      chinese: 0
    }
    Object.keys(formatMap).forEach(key => {
      if (formatArray.includes(key)) formatMap[key] = 1
    })
    let formatReg = new RegExp(
      `^[${formatMap.number ? '0-9' : ''}${
        formatMap.letter ? 'a-zA-Z' : ''
      }${formatMap.chinese ? '\u4e00-\u9fa5' : ''}]*$`
    )
    if (!formatReg.test(this.input)) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
  // 郵箱校驗
  isEmail(message) {
    if (!this.pass) return this
    const emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
    if (!emailReg.test(this.input)) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
  // ulr校驗
  isURL(message) {
    if (!this.pass) return this
    const urlReg = new RegExp(
      '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
      'i'
    )
    if (!urlReg.test(this.input)) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
  // 自定義正則校驗
  requireRegexp(reg, message) {
    if (!this.pass) return this
    if (!reg.test(this.input)) {
      this.errorMessage = message
      this.pass = false
    }
    return this
  }
}

使用姿勢如下:

import Coi from 'js-coi'
const validCoi = new Coi()
validCoi
  .data('1234')
  .isRequired('id不能為空')
  .minLength(3, 'id不能少于3位')
  .maxLength(5, 'id不能多于5位')
  .data('1234@qq.')
  .isRequired('郵箱不能為空')
  .isEmail('郵箱格式不正確')
  .data('http:dwd')
  .isRequired('url不能為空')
  .isUrl('url格式不正確')
if (!validCoi.pass) {
  this.$message.error(validCoi.errorMessage)
  return
}

當然你只校驗一個字段的話也可以這么使用:

import Coi from 'js-coi'
const idCoi = new Coi('1234')
idCoi
  .isRequired('id不能為空')
  .minLength(3, 'id不能少于3位')
  .maxLength(5, 'id不能多于5位')
  .isEmail('id郵箱格式不正確')
  .isUrl('id格式不正確')
  .requireFormat(['number', 'letter', 'chinese'], 'id格式不正確')
  .requireRegexp(/012345/, 'id格式不正確')
if (!idCoi.pass) {
  this.$message.error(idCoi.errorMessage)
  return
}

總結(jié)

以上所述是小編給大家介紹的一百行JS代碼實現(xiàn)一個校驗工具,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論

广宗县| 平山县| 龙岩市| 无棣县| 汉源县| 枞阳县| 读书| 济南市| 民权县| 吉木乃县| 郑州市| 宾阳县| 奈曼旗| 石楼县| 平湖市| 阆中市| 凤冈县| 开阳县| 遂宁市| 陆丰市| 阜宁县| 赣榆县| 保定市| 南江县| 仁寿县| 奉新县| 宁河县| 资阳市| 法库县| 镇康县| 泰兴市| 康定县| 土默特左旗| 克拉玛依市| 堆龙德庆县| 曲沃县| 拜城县| 探索| 合江县| 普兰店市| 古浪县|