手机号码校验封装

192 阅读1分钟
/**
	 * @description 手机号码校验
	 * @param {number} phone 手机号
	 */
	phoneRegex(phone){
		if(phone == ''){
                        uni.showToast({
                                title: '手机号码不能为空',
                                duration: 2000
                        });
			return false;
		}
		if(phone.length !== 11){
                          uni.showToast({
                                title: '手机号码长度错误',
                                duration: 2000
                        });
			return false;
		}
		let myreg = 
		/^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|17[0-9]{1}|(18[0-9]{1}|19[0-9]{1}))+\d{8})$/; //验证手机号
		if(myreg.test(phone)){
			return true;
		}else{
                         uni.showToast({
                                title: '请输入有效手机号码',
                                duration: 2000
                        });
			
			return false;
		}
	},

使用:

import Util from '@/common/util.js'  //先导入
async getPhone() {
let phonereg = await Util.phoneRegex(this.phone) 
				if(!phonereg) {
					console.log("手机号不正确");
					return 
				}
                 }