JAva携帯電話番号、固定電話の合法性を検証する


一、ルールの説明
1、全国の固定電話番号は大きく2種類に分けられ、3桁と4桁(400固定語を除く)の中国の電話番号の3桁は10大都市がある.北京市010広州市020上海市021天津市022重慶市023瀋陽市024南京市025武漢市027成都市028西安市029 2、携帯電話は国の携帯電話番号の上位3位が更新を続けているため、現在は移動、聯通、電気通信の3大事業者の携帯電話番号は大体以下の通りである:移動番号は1341351361371381139145151152157158159178183184187188である.インターコネクト番号セグメントは13013132121155156185186145176です.電気信号セグメントは13331531717180181189である.中国で使用されている携帯電話の番号は11位で、その中の各セグメントには異なる符号化方向がある:上位3位--ネットワーク識別番号;4-7位--地域コード;8~11-ユーザー番号
二、工具類
import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

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

/**
 *    :   、         
 */

public class PhoneValidatorUtil {

    public static boolean matchPhone(String number, int type){
        if (TextUtils.isEmpty(number)) {
            return false;
        }
        if (type == 0) {
            Matcher m = null;
            Pattern p = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  //       ,   "-"
            //p = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         //        
            m = p.matcher(number);
            return m.matches();
        } else {
            //    true
            if (Pattern.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|17[0|6|7|8])\\d{8}$", number)) {
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @param context
     * @param number
     * @param type 0    1    
     * @return
     */
    public static boolean matchPhoneShowToast(Context context, String number, int type){
        boolean flag = matchPhone(number, type);
        if (!flag){
            Toast.makeText(context, type == 0 ? "        " : "        !",
                    Toast.LENGTH_SHORT).show();
        }
        return flag;
    }
}

このツール類は現在13、14、15、17、18の先頭の番号をサポートしており、現在市販されているすべての携帯電話番号を基本的にサポートすることができる.後期に新しい番号があれば、さらに改善する必要がある.以下はテスト例である.
//      String number = "0557-5033061";
//      String number = "021-62963636";
        String number = "029-86261199";
        if (PhoneValidatorUtil.matchPhoneShowToast(this, number, 0)){
            Log.i("TAG", "        !");
        } else {
            Log.i("TAG", "        !");
        }
        String phone = "14755296414";
        if (PhoneValidatorUtil.matchPhoneShowToast(this, phone, 1)){
            Log.i("TAG", "        !");
        } else {
            Log.i("TAG", "        !");
        }