JS实用正则校验

是否为合法uri 

/* 合法uri */
export function validURL(url) {

  const reg =

   /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/

  return reg.test(url)

}

是否为小写字母

/* 小写字母 */

export function validLowerCase(str) {

  const reg = /^[a-z]+$/

  return reg.test(str)

}

是否为大写字母

/* 大写字母 */

export function validUpperCase(str) {

  const reg = /^[A-Z]+$/

  return reg.test(str)

}

是否为大小写字母

/* 大小写字母 */

export function validAlphabets(str) {

  const reg = /^[A-Za-z]+$/

  return reg.test(str)

}

是否为邮箱

/* 邮箱 */

export function validEmail(email) {

  const re =

   /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

  return re.test(email)

}

是否为手机号码

/* 手机号码 */

export function validatePhone(phone) {

  const reg = /^[1][3,4,5,7,8][0-9]{9}$/

  return reg.test(phone)

}

是否包含中文

/* 是否包含中文 */

export function validateCh(value) {

  const reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/

  return reg.test(value)

}

末尾是否以mp3或者mp4结尾

/* 末尾是否以mp3或者mp4结尾 */

export function validateMp(value) {

  const reg=/\.(mp4|mp3)$/

  return reg.test(value)

}

是否为2-4个中文字符,多用于姓名昵称

//2-4个中文字符正则

export function validateName(value) {

  const reg=/^[\u4e00-\u9fa5]{2,4}$/

  return reg.test(value)

}

是否为身份证

//是否为身份证

export function validateId(value) {

  const reg=/^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/

  return reg.test(value)

}

猜你喜欢

转载自blog.csdn.net/yxlyttyxlytt/article/details/129816076
今日推荐