Java正则表达式——验证手机号和电话号码



[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.   * 获取当前的httpSession 
  3.   * @author :shijing 
  4.   * 2016年12月5日下午3:46:02 
  5.   * @return 
  6.   */  
  7.  public static HttpSession getSession() {  
  8.    return getRequest().getSession();  
  9.  }  
  10.    
  11.  /** 
  12.   * 手机号验证 
  13.   * @author :shijing 
  14.   * 2016年12月5日下午4:34:46 
  15.   * @param  str 
  16.   * @return 验证通过返回true 
  17.   */  
  18.  public static boolean isMobile(final String str) {  
  19.      Pattern p = null;  
  20.      Matcher m = null;  
  21.      boolean b = false;  
  22.      p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号  
  23.      m = p.matcher(str);  
  24.      b = m.matches();  
  25.      return b;  
  26.  }  
  27.  /** 
  28.   * 电话号码验证 
  29.   * @author :shijing 
  30.   * 2016年12月5日下午4:34:21 
  31.   * @param  str 
  32.   * @return 验证通过返回true 
  33.   */  
  34.  public static boolean isPhone(final String str) {  
  35.      Pattern p1 = null, p2 = null;  
  36.      Matcher m = null;  
  37.      boolean b = false;  
  38.      p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的  
  39.      p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的  
  40.      if (str.length() > 9) {  
  41.         m = p1.matcher(str);  
  42.         b = m.matches();  
  43.      } else {  
  44.          m = p2.matcher(str);  
  45.         b = m.matches();  
  46.      }  
  47.      return b;  
  48.  }  
  49.    
  50.  public static void main(String[] args) {  
  51.    String phone = "13900442200";  
  52.    String phone2 = "021-88889999";  
  53.    String phone3 = "88889999";  
  54.    String phone4 = "1111111111";  
  55.    //测试1  
  56.    if(isPhone(phone) || isMobile(phone)){  
  57.      System.out.println("1这是符合的");  
  58.    }  
  59.    //测试2  
  60.    if(isPhone(phone2) || isMobile(phone2)){  
  61.      System.out.println("2这是符合的");  
  62.    }  
  63.    //测试3  
  64.    if(isPhone(phone3) || isMobile(phone3)){  
  65.      System.out.println("3这是符合的");  
  66.    }  
  67.    //测试4  
  68.    if(isPhone(phone4) || isMobile(phone4)){  
  69.      System.out.println("4这是符合的");  
  70.    }else{  
  71.      System.out.println("不符合");  
  72.    }  
  73.  }  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.   * 获取当前的httpSession 
  3.   * @author :shijing 
  4.   * 2016年12月5日下午3:46:02 
  5.   * @return 
  6.   */  
  7.  public static HttpSession getSession() {  
  8.    return getRequest().getSession();  
  9.  }  
  10.    
  11.  /** 
  12.   * 手机号验证 
  13.   * @author :shijing 
  14.   * 2016年12月5日下午4:34:46 
  15.   * @param  str 
  16.   * @return 验证通过返回true 
  17.   */  
  18.  public static boolean isMobile(final String str) {  
  19.      Pattern p = null;  
  20.      Matcher m = null;  
  21.      boolean b = false;  
  22.      p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号  
  23.      m = p.matcher(str);  
  24.      b = m.matches();  
  25.      return b;  
  26.  }  
  27.  /** 
  28.   * 电话号码验证 
  29.   * @author :shijing 
  30.   * 2016年12月5日下午4:34:21 
  31.   * @param  str 
  32.   * @return 验证通过返回true 
  33.   */  
  34.  public static boolean isPhone(final String str) {  
  35.      Pattern p1 = null, p2 = null;  
  36.      Matcher m = null;  
  37.      boolean b = false;  
  38.      p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的  
  39.      p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的  
  40.      if (str.length() > 9) {  
  41.         m = p1.matcher(str);  
  42.         b = m.matches();  
  43.      } else {  
  44.          m = p2.matcher(str);  
  45.         b = m.matches();  
  46.      }  
  47.      return b;  
  48.  }  
  49.    
  50.  public static void main(String[] args) {  
  51.    String phone = "13900442200";  
  52.    String phone2 = "021-88889999";  
  53.    String phone3 = "88889999";  
  54.    String phone4 = "1111111111";  
  55.    //测试1  
  56.    if(isPhone(phone) || isMobile(phone)){  
  57.      System.out.println("1这是符合的");  
  58.    }  
  59.    //测试2  
  60.    if(isPhone(phone2) || isMobile(phone2)){  
  61.      System.out.println("2这是符合的");  
  62.    }  
  63.    //测试3  
  64.    if(isPhone(phone3) || isMobile(phone3)){  
  65.      System.out.println("3这是符合的");  
  66.    }  
  67.    //测试4  
  68.    if(isPhone(phone4) || isMobile(phone4)){  
  69.      System.out.println("4这是符合的");  
  70.    }else{  
  71.      System.out.println("不符合");  
  72.    }  
  73.  }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值