10 分析電話電話號(hào)碼庫(kù)

    <dependency>
            <groupId>com.googlecode.libphonenumber</groupId>
            <artifactId>libphonenumber</artifactId>
            <version>8.13.47</version>
        </dependency>

編寫校驗(yàn)邏輯:
使用libphonenumber庫(kù)中的PhoneNumberUtil類來解析和驗(yàn)證電話號(hào)碼。

package com.lvue.springbootplus.ip;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;

public class PhoneNumberValidator {
    public static boolean isValidPhoneNumber(String phoneNumber, String regionCode) {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        try {
            PhoneNumber numberProto = phoneUtil.parse(phoneNumber, regionCode);
            return phoneUtil.isValidNumber(numberProto);
        } catch (NumberParseException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String phoneNumber = "+8613800138000"; // 包含國(guó)家代碼的國(guó)際格式
        String regionCode = "CN"; // 國(guó)家代碼,例如中國(guó)的國(guó)家代碼是CN或86
        if (isValidPhoneNumber(phoneNumber, regionCode)) {
            System.out.println(phoneNumber + " is a valid phone number.");
        } else {
            System.out.println(phoneNumber + " is NOT a valid phone number.");
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容