网络营销网站开发,设计公司网站推广营销,素材天下,律师事务所网站方案关于如何在不同国家#xff08;例如美国#xff0c;美国#xff09;使用Java验证电话号码的快速指南。 带有正则表达式和Google libphonenumber API的示例程序。 1.简介 在本教程中#xff0c;我们将学习如何在java中验证电话号码 。 这主要是为了验证美国和印度的国家/地… 关于如何在不同国家例如美国美国使用Java验证电话号码的快速指南。 带有正则表达式和Google libphonenumber API的示例程序。 1.简介 在本教程中我们将学习如何在java中验证电话号码 。 这主要是为了验证美国和印度的国家/地区电话号码但是在看到示例之后您可以为其他国家/地区开发验证规则。 这是验证移动电话号码的常见要求因为我们会进行电子邮件地址验证的验证但是Java没有内置功能来提供此类方法。 但是我们可以借助正则表达式和带有libphonenumber的google api来实现这一点。 让我们跳入编写示例程序。 2.正则表达式 正则表达式的实现有些棘手因为电话号码具有多种格式且具有不同的扩展名。 例如以下是一些常见的写电话号码的方法 美国 。 1234567890 123 - 456 - 7890 123 - 456 - 7890 x1234 123 - 456 - 7890 ext1234 ( 123 )- 456 - 7890 123.456 . 7890 123 456 7890 印度 9123124123 使用正则表达式验证电话号码 package com.java.w3schools.blog.java.program.to.libphonenumber; public class PhoneValidationRegularExpression { public static void main(String[] args) { System.out.println( Validation for 1234567890 : validatePhoneNumber( 1234567890 )); System.out.println( Validation for 1234 567 890 : validatePhoneNumber( Validation for 1234 567 890 : validatePhoneNumber( 1234 567 890 )); System.out.println( Validation for 123 456 7890 : validatePhoneNumber( Validation for 123 456 7890 : validatePhoneNumber( 123 456 7890 )); System.out.println( Validation for 123-567-8905 : validatePhoneNumber( 123-567-8905 )); System.out.println( Validation for 9866767545 : validatePhoneNumber( 9866767545 )); System.out.println( Validation for 123-456-7890 ext9876 : validatePhoneNumber( 123-456-7890 ext9876 )); } private static boolean validatePhoneNumber(String phoneNumber) { // validate phone numbers of format 1234567890 if (phoneNumber.matches( \\d{10} )) return true ; // validating phone number with -, . or spaces else if (phoneNumber.matches( \\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4} )) return true ; // validating phone number with extension length from 3 to 5 else if (phoneNumber.matches( \\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5} )) return true ; // validating phone number where area code is in braces () else if (phoneNumber.matches( \\(\\d{3}\\)-\\d{3}-\\d{4} )) return true ; // Validation for India numbers else if (phoneNumber.matches( \\d{4}[-\\.\\s]\\d{3}[-\\.\\s]\\d{3} )) return true ; else if (phoneNumber.matches( \\(\\d{5}\\)-\\d{3}-\\d{3} )) return true ; else if (phoneNumber.matches( \\(\\d{4}\\)-\\d{3}-\\d{3} )) return true ; // return false if nothing matches the input else return false ; } } 输出 Validation for 1234567890 : true Validation for 1234 567 890 : true Validation for 123 456 7890 : true Validation for 123 - 567 - 8905 : true Validation for 9866767545 : true Validation for 123 - 456 - 7890 ext9876 : true 3. Google libphonenumber示例 如果您使用正则表达式方法则需要进行大量测试以涵盖所有极端情况。 但是如果我们有一些API可以通过适当的测试来提供此功能那么可以在我们的应用程序中很好地使用它。 Google将 libphonenumber API 作为一个开放源代码库提供该库提供了诸如解析格式化验证和存储国际电话号码的功能 。 此API经过优化可在智能手机上运行并且也可用于Android框架。 此API的主要优点是您可以格式化或验证和解析任何国家或地区的手机号码。 PhoneNumberUtil类是用于国际电话号码的实用程序。 功能包括格式化解析和验证。 package com.java.w3schools.blog.java.program.to.libphonenumber; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; public class PhoneValidationWIthGooglelibphonenumberAPI { public static void main(String[] args) throws NumberParseException { PhoneNumberUtil numberUtil PhoneNumberUtil.getInstance(); PhoneNumber phoneNumber numberUtil.parse( 9866123456 , IN ); boolean isValid numberUtil.isValidNumber(phoneNumber); System.out.println( Checking given phone number is vaid : isValid); } } 输出 Checking given phone number is vaid : true 验证美国或印度电话号码的另一个示例 样本电话号码 package com.java.w3schools.blog.java.program.to.libphonenumber; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; public class PhoneValidationWIthGooglelibphonenumberAPI { public static void main(String[] args) throws NumberParseException { System.out.println( USA phone number validations: ); String[] usPhoneNumbers { (541) 754-3010 , 1-541-754-3010 , 1-541-754-3010 , 001-541-754-3010 , 191 541 754 3010 }; validatePhoneNumber(usPhoneNumbers, US ); System.out.println( \nindia phone number validations: ); String[] indiaPhoneNumbers { 91 7503907302 , 9702522865 , 91–8477812345 , 91 9999999999 , 91 9688 123 456 , 9688 123 456 }; validatePhoneNumber(indiaPhoneNumbers, IN ); } private static void validatePhoneNumber(String[] phoneNumbers, String region) throws NumberParseException { PhoneNumberUtil numberUtil PhoneNumberUtil.getInstance(); for (String number : phoneNumbers) { PhoneNumber phoneNumber numberUtil.parse(number, region); boolean isValid numberUtil.isValidNumber(phoneNumber); if (isValid) { System.out.println(number is a valid number. ); } else { System.out.println(number is a not valid number. ); } } } } 输出 USA phone number validations: ( 541 ) 754 - 3010 is a valid number. 1 - 541 - 754 - 3010 is a valid number. 1 - 541 - 754 - 3010 is a valid number. 001 - 541 - 754 - 3010 is a not valid number. 191 541 754 3010 is a not valid number. india phone number validations: 91 7503907302 is a valid number. 9702522865 is a valid number. 91 – 8477812345 is a valid number. 91 9999999999 is a valid number. 91 9688 123 456 is a valid number. 9688 123 456 is a not valid number. PhoneNumberUtil API 5. PhoneNumberType 此API支持以下所有类型的数字 。 FIXED_LINE FIXED_LINE_OR_MOBILE MOBILE PAGER PERSONAL_NUMBER PREMIUM_RATE SHARED_COST TOLL_FREE UAN UNKNOWN VOICEMAIL VOIP PhoneNumberUtil.PhoneNumberType 六结论 在本文中我们已经看到了如何使用regex和Google开源api libphonenumber在Java中验证电话号码 。 GitHub代码 Google API 翻译自: https://www.javacodegeeks.com/2020/04/how-to-validate-phone-numbers-in-java-regular-expression-google-libphonenumber.html