![](https://i-blog.csdnimg.cn/blog_migrate/1dda6033971ccabb574b4fdee8f7a12f.png)
主要介绍下uniApp中正则的使用,以校验手机、邮箱为例。
//util.js文件主要写的是会经常使用到的工具类
//校验邮箱格式
function checkEmail(email){
return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(email);
}
//校验手机格式
function checkMobile(mobile){
return RegExp(/^1[34578]\d{9}$/).test(mobile);
}
module.exports = {
checkEmail : checkEmail,
checkMobile : checkMobile
}
//register.js
//点击获取验证码,校验手机号和邮箱格式
obtain(){
if(this.table == 'tel'){
if(!until.checkMobile(this.mobile)){
uni.showToast({title: '手机号格式错误',icon: 'none'});
return;
}
this.getMobileVerify()
}else if(this.table == 'email'){
if(!until.checkEmail(this.email)){
uni.showToast({title: '邮箱格式错误',icon: 'none'});
return;
}
this.getEmailVerify()
}
},