欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中验证国际手机号码

程序员文章站 2022-03-02 11:54:42
...

最近有QA的同事反馈需要在后台验证手机号码的有效性,由于项目的国际化,所以用简单的正则表达式很难实现,最后发现采用谷歌的libphonenumber可以很好地的很方便的实现。

首先我们需要引入如下几个包

    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);
 }