1. 程式人生 > >java中驗證國際手機號碼

java中驗證國際手機號碼

首先我們需要引入如下幾個包

    compile('com.googlecode.libphonenumber:geocoder:2.104')
    compile('com.googlecode.libphonenumber:libphonenumber:8.9.15')
    compile('com.googlecode.libphonenumber:carrier:1.94')
    compile('com.googlecode.libphonenumber:prefixmapper:2.104')

新增依賴包以後我們就可以很方便實現以下驗證程式碼:

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;

/**
 * @author longshelan
 * @date 2018/11/1 13:50
 */
public class PhoneUtil {
    private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * check phone
     *
     * @param phoneNumber eg" 18012345678"
     * @param countryCode eg "86"
     * @return the result "true" or "false"
     */
    public static boolean checkPhoneNumber(String phoneNumber, Integer countryCode) {

        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);

        return phoneNumberUtil.isValidNumber(pn);
    }

    /**
     * check phone that it brings country code
     *
     * @param phoneNumber eg" +8618012345678"
     * @return the result "true" or "false"
     * @throws NumberParseException handle phone that it can't resolve
     */
    public static boolean checkPhoneNumberBringCountryCode(String phoneNumber) throws NumberParseException {
        Phonenumber.PhoneNumber cn = phoneNumberUtil.parse(phoneNumber, "CN");
        return phoneNumberUtil.isValidNumber(cn);
    }
}

上述兩個方法分別可檢驗含有國際程式碼的手機號和普通的傳入電話號碼、國家程式碼的驗證,如需判斷運營商和手機號碼的歸屬地,可採用以下方法:

  /**
     * 根據國家程式碼和手機號  判斷手機運營商
     * @date 2017-4-26 上午11:30:18
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static String getCarrier(String phoneNumber, Integer countryCode){
 
        long phone = Long.parseLong(phoneNumber);
 
        PhoneNumber pn = new PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);
        //返回結果只有英文,自己轉成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        carrierZh += geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
        switch (carrierEn) {
        case "China Mobile":
            carrierZh += "移動";
            break;
        case "China Unicom":
            carrierZh += "聯通";
            break;
        case "China Telecom":
            carrierZh += "電信";
            break;
        default:
            break;
        }
        return carrierZh;
    }
 
 
    /**
     * 
    * @Description: 根據國家程式碼和手機號  手機歸屬地
    * @date 2017-4-26 上午11:33:18
    * @param phoneNumber
    * @param countryCode
    * @return    引數
     */
    public static String getGeo(String phoneNumber, Integer countryCode){
 
        long phone = Long.parseLong(phoneNumber);
 
        PhoneNumber pn = new PhoneNumber();
        pn.setCountryCode(countryCode);
        pn.setNationalNumber(phone);
 
        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
 }