js commonly used utils functions

1. Space and null character handling

    In some interactive operations, such as input boxes, we need to limit spaces and empty strings

const isNull = (str) => {
  if (str === '') return true
  let reg = /^[ ]+$/
  return reg.test(str)
}

    When our front-end displays user information, we need to protect the user's information, such as name, ID card, etc.

 2. Name handling

const handleName = (str) => {
  if (str !== null && str !== undefined) {
    let star = '' // 用于存放名字中间的 *

    // 名字两位就将最后一位设为 *
    if (str.length <= 2) {
      return str.substring(0, 1) + '*'
    } else {
      // 循环姓名长度,如果名字长度大于2位,将中间的设为 *
      for (let i = 0; i < str.length - 2; i++) {
        star = star + '*'
      }
      return str.substring(0, 1) + star + str.substring(str.length - 1, str.length)
    }
  }
}

3. ID number processing

const handleCardId = (str) => {
   return str.replace(/(?<=\d{6})\d{8}(?=\d{2})/, '*********')
}

4. Mobile phone number processing

const validMobile = (val) => {
  var tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
  return tel.test(val)
}

Guess you like

Origin blog.csdn.net/weixin_52020362/article/details/131269013