微信小程序 - 校验用户输入的手机号 / 身份证号 / 邮箱(正则表达式)

前言

这里提供了正则表达式的校验方法,具体业务逻辑请自行更改。

手机号校验

phone 为用户输入的手机号。

if (!(/^1[34578]\d{9}$/.test(phone))) {
  console.log("手机号错误!")
}

else {
  console.log("手机号正确!")
}

身份证号校验

id 为用户输入的身份证号。

if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(id))) {
  console.log("身份证号错误!")
}

else {
  console.log("身份证号正确!")
}

邮箱号校验

emlia 为用户输入的邮箱号。

if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(emlia))) {
  console.log("邮箱号错误!")
}

else {
  console.log("邮箱号正确!")
}

猜你喜欢

转载自blog.csdn.net/weixin_44198965/article/details/108056575