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

peke 8年前發布 | 4K 次閱讀 Java

/**

     * 手機號驗證
     * 
     * @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,7,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;
    }</pre> 


 本文由用戶 peke 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!