java、js验证是否是正确的手机号码


我国手机运营商的手机代码标准格式为:国家码+收集号码,例如8613912345678,特点如下:
1、长度13位
2、以86的国家码打头
3、手机的每一位都是数字
请实现手机号码合法性判断的函数,要求实现个判断,返回值和优先级如下:
1、如果手机号码合法,返回0
2、如果手机长度不合法,返回1
3、如果手机号码中包含非数字字符,返回2
4、如果手机号码不是86打头的,返回3

java实现

/**
 * @author zql
 *
 */
public class JudgeMobile {
	
	/**
	 * 验证是否是正确的手机
	 * 
	 * @param mobile 手机号码
	 * @return 
	 */
	public static int algorithm1(String mobile) {
		if (mobile.length() != 13) {
			return 1;
		}
		if (!"86".equals(mobile.substring(0, 2))) {
			return 3;
		}
		mobile = mobile.substring(2);
		/*
		 * ^ 表示字符串开始
		 * $ 表示字符串结束
		 * [0-9] 代表0到9的数字
		 * * 表示多个字符
		 * 
		 */
		// 包含非数字的字符
		if (!mobile.matches("^[0-9]*$")) {
			return 2;
		}
	    return 0;
	}
	
	/**
	 * 
	 * @param mobile 手机号码
	 * @return
	 */
	public static int algorithm2(String mobile) {
		if (mobile.length() != 13) {
			return 1;
		}
		/*
		 * 字符参与运算,其实是拿字符对应的ASCII码表中的数字进行运算的
		 * 而8对应的ASCII码是56,6对应的ASCII码是54,因此下列if条件
		 * 中,应写魔法值56和54
		 */
		if (!(mobile.charAt(0) == 56 && mobile.charAt(1) == 54)) {
			return 3;
		}
		for (int i = 2; i < mobile.length(); i++) {
			/*
			 * isDigit()方法判断字符是否为数字
			 */
			if (!Character.isDigit(mobile.charAt(i))) {
				return 2;
			}
		}
		return 0;
	}
	
	/**
	 * 实际项目使用
	 * 验证是否是正确的手机
	 * 
	 * @param mobile 手机号码
	 * @return 
	 */
	public static int isMobile(String mobile) {
		if (mobile.length() != 13) {
			return 1;
		}
		if (!"86".equals(mobile.substring(0, 2))) {
			return 3;
		}
		mobile = mobile.substring(2);
		// 判断是否是三大运营商规定的标准手机格式
		if (!mobile.matches("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$")) {
	    	return 2;
		}
	    return 0;
	}
	
	public static void main(String[] args) {
		String s1 = "123";
		String s2 = "12345678910111213";
		String s3 = "5512345678910";
		String s4 = "8512345678910";
		String s5 = "5612345678910";
		String s6 = "8612345678910";
		String s7 = "8613812345678";
		String s8 = "86138123 5678";
		String s9 = "861381234567 ";
		String s10 = "861381234567a";
		String s11 = "8613812a45678";
		System.out.println(algorithm1(s1) + " | " + algorithm2(s1) + " | " + isMobile(s1));
		System.out.println(algorithm1(s2) + " | " + algorithm2(s2) + " | " + isMobile(s2));
		System.out.println(algorithm1(s3) + " | " + algorithm2(s3) + " | " + isMobile(s3));
		System.out.println(algorithm1(s4) + " | " + algorithm2(s4) + " | " + isMobile(s4));
		System.out.println(algorithm1(s5) + " | " + algorithm2(s5) + " | " + isMobile(s5));
		System.out.println(algorithm1(s6) + " | " + algorithm2(s6) + " | " + isMobile(s6));
		System.out.println(algorithm1(s7) + " | " + algorithm2(s7) + " | " + isMobile(s7));
		System.out.println(algorithm1(s8) + " | " + algorithm2(s8) + " | " + isMobile(s8));
		System.out.println(algorithm1(s9) + " | " + algorithm2(s9) + " | " + isMobile(s9));
		System.out.println(algorithm1(s10) + " | " + algorithm2(s10) + " | " + isMobile(s10));
		System.out.println(algorithm1(s11) + " | " + algorithm2(s11) + " | " + isMobile(s11));
	}
	
}

javascript实现

/**
 * 此方法仅满足第3要求,如果手机号码中包含非数字字符,返回2
 * 
 * @param {Object} mobile
 */
function algorithm(mobile){
	// 先转为字符串,因为如果传入得是一个数字类型的值,它的长度会是undefined,js数字类型是没有长度的
	mobile = mobile + "";
	if (mobile.length != 13) {
		return 1;
	}
	if (mobile.substring(0,2) != "86") {
		return 3;
	}
	mobile = mobile.substring(2);
	for (var i = 0; i < mobile.length; i++) {
		var reg = /\d/;
		// 判断是否有非数字
		if (!reg.test(mobile[i])) {
			return 2;
		}
	}
	return 0;
}
/**
 * 实现项目中使用,判断手机号是否正确
 * 
 * @param {Object} mobile 
 */
function isMobile(mobile) {
	mobile = mobile + "";
	if (mobile.length != 13) {
		return 1;
	}
	if (mobile.substring(0,2) != "86") {
		return 3;
	}
	mobile = mobile.substring(2);
	var reg = /^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\d{8}$/;
	// 含有非数字或格式不正确
	if(!reg.test(mobile)){
		return 2;
	} else{
		return 0
	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值