Java電話號碼和手機號碼正則驗證

  1. /** 
  2.      * 手機號驗證 
  3.      *  
  4.      * @param  str 
  5.      * @return 驗證通過返回true 
  6.      */  
  7.     public static boolean isMobile(String str) {   
  8.         Pattern p = null;  
  9.         Matcher m = null;  
  10.         boolean b = false;   
  11.         p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 驗證手機號  
  12.         m = p.matcher(str);  
  13.         b = m.matches();   
  14.         return b;  
  15.     }  
  16.     /** 
  17.      * 電話號碼驗證 
  18.      *  
  19.      * @param  str 
  20.      * @return 驗證通過返回true 
  21.      */  
  22.     public static boolean isPhone(String str) {   
  23.         Pattern p1 = null,p2 = null;  
  24.         Matcher m = null;  
  25.         boolean b = false;    
  26.         p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 驗證帶區號的  
  27.         p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 驗證沒有區號的  
  28.         if(str.length() >9)  
  29.         {   m = p1.matcher(str);  
  30.             b = m.matches();    
  31.         }else{  
  32.             m = p2.matcher(str);  
  33.             b = m.matches();   
  34.         }    
  35.         return b;  
  36.     }  
  37. 點擊打開鏈接
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章