js funciones utils de uso común

1. Manejo de espacios y caracteres nulos

    En algunas operaciones interactivas, como los cuadros de entrada, necesitamos limitar espacios y cadenas vacías

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

    Cuando nuestro front-end muestra la información del usuario, debemos proteger la información del usuario, como el nombre, la tarjeta de identificación, etc.

 2. Manejo de nombres

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. Procesamiento del número de identificación

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

4. Procesamiento de números de teléfonos móviles

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)
}

Supongo que te gusta

Origin blog.csdn.net/weixin_52020362/article/details/131269013
Recomendado
Clasificación