YoRenChen / Blog

web blog
MIT License
7 stars 1 forks source link

前端校验规则参考 #1

Open YoRenChen opened 3 years ago

YoRenChen commented 3 years ago

前端校验规则参考 (持续填充 🌊🌊🌊)

密码校验

8-16位密码,至少包含数字、大小写字母、特殊字符中任意两种

正则:/(?!^(\d+|[a-zA-Z]+|[~!@#$%^&?]+)$)^[\w~!@#$%^&?]{8,16}$/.test('这里输入你想要的')

手机号校验

正则:/^1[3-9]\d{9}$/

匹配为汉字、字母、数字

/^[0-9\u4e00-\u9fa5a-z]+$/gi

u4e00 - u9fa5a中文
-z 字母
gi:g全局、i忽略大小写

时间戳差值转换为HH:MM:SS时间格式

/**
* timeStamp: 毫秒级别时间差值
* return HH:MM:SS
* eg: durationTime(19000000) => "05:16:40"
*/
function durationTime(timeStamp) {
  if (isNaN(+timeStamp)) return 'Errer timeStamp'
  if (timeStamp < 0) timeStamp = Math.abs(timeStamp)
  return Array(3)
    .fill(parseInt(timeStamp / 1000))
    .reduce((total, val, i, arr) => {
      const num = parseInt((val / Math.pow(60, (arr.length - 1 - i))) -
          (!i ? 0 : total[0] * Math.pow(60, i)) -
          (i < 2 ? 0 : total[1] * Math.pow(60, i - 1)))
          return total.concat(`0${num}`.slice(-2))
      }, [])
    .toString()
    .replaceAll(',', ':')
}
或者: 
function durationTime(timeStamp) {
  if (isNaN(+timeStamp)) return 'Errer timeStamp'
  if (timeStamp < 0) timeStamp = Math.abs(timeStamp)
  const t = parseInt(timeStamp / 1000)
  const hour = parseInt(t / 60 / 60)
  const min = parseInt((t / 60) - hour * 60)
  const sec = parseInt(t - hour * 60 * 60 - min * 60)
  return [hour, min, sec].map(e => e.toString().length > 1 ? e : `0${e}`).toString().replaceAll(',', ':')
}