前言
这里提供了正则表达式的校验方法,具体业务逻辑请自行更改。
手机号校验
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("邮箱号正确!")
}