首页 > 移动平台 > 详细

Java工具类:(1)判断String是否为手机号码

时间:2016-06-16 08:01:59      阅读:821      评论:0      收藏:0      [点我收藏+]


判断是否为手机号码和电话号码

package com.rk.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils
{
	/**
	 * 手机号验证
	 * 
	 * @param str
	 * @return 验证通过返回true
	 */
	public static boolean isMobile(String str)
	{
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
		p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
		m = p.matcher(str);
		b = m.matches();
		return b;
	}

	/**
	 * 电话号码验证
	 * 
	 * @param str
	 * @return 验证通过返回true
	 */
	public static boolean isPhone(String str)
	{
		Pattern p1 = null, p2 = null;
		Matcher m = null;
		boolean b = false;
		p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
		p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
		if (str.length() > 9)
		{
			m = p1.matcher(str);
			b = m.matches();
		}
		else
		{
			m = p2.matcher(str);
			b = m.matches();
		}
		return b;
	}
}


Java工具类:(1)判断String是否为手机号码

原文:http://lsieun.blog.51cto.com/9210464/1789752

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!