美文网首页
函数正则总结

函数正则总结

作者: 特小懒虫 | 来源:发表于2020-04-15 15:06 被阅读0次

1、验证金额(输入整数,小数点后一或两位)

// 小数点后两位
export function CheckAmount (str) {
  let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  return reg.test(str)
}
// 小数点后一位
export function CheckOnePoint (str) {
  let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1})?$)|(^(0){1}$)|(^[0-9]\.[0-9]?$)/
  return reg.test(str)
}

2、验证身份证号

export function validateIDcard (str) {
  const reg = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/
  return reg.test(str)
}

3、验证邮箱

export function CheckEmail(email){
  return (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/.test(email))
}

4、手机号简单验证

export function CheckPhone(phone){ 
  return (/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone))
}

5、特殊字符和空格判断 如果有返回true

export function CheckString (str) {
  let reg = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
  let regSpace = /\s/g
  return reg.test(str) || regSpace.test(str)
}

6、时间的一些转换

// 20180829105739 ====> 2018-08-29-10:57:39
export function changeTime (date) {
  return `${date.slice(0, 4)}-${date.slice(4, 6)}-${date.slice(6, 8)} ${date.slice(8, 10)}:${date.slice(10, 12)}:${date.slice(12, 14)}`
}
// 20180829 ====> 2018-08-29
export function changeResponseDate (date = '') {
  return `${date.slice(0, 4)}-${date.slice(4, 6)}-${date.slice(6, 8)}`
}
// 1546939193000 ===> 2019-01-08 17:19:53
export function formatTime (date) {
  if (!date) {
    return ''
  }
  date = new Date(date);
  let day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
  let hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours()
  let minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes()
  let second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds()
  let month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1)
  return `${date.getFullYear()}-${month}-${day} ${hour}:${minute}:${second}`
}
// 'Thu May 12 2017 08:00:00 GMT+0800 (中国标准时间)' =>> yyyy-MM-dd hh:mm:ss
export function grpcDateFormat (date) {
  if (!date) {
    return ''
  }
  date = new Date(date);
  let day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
  let hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours()
  let minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes()
  let second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds()
  let month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1)
  return `${date.getFullYear()}-${month}-${day} ${hour}:${minute}:${second}`
}

相关文章

网友评论

      本文标题:函数正则总结

      本文链接:https://www.haomeiwen.com/subject/qyscvhtx.html