Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check phone number format is valid or not from telephony manager?

For checking the phone number(I have the pattern phone number like +918172014908) validation I use libphonenumber.jar file.. It checks the phone number according to the country is valid or not. I use this:--

 PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 
         PhoneNumber numberProto = phoneUtil.parse("phone_number", "");  
        phoneUtil.isValidNumber(numberProto) == true ? "valid" : "phone no not valid"

Its working fine..But this jar file takes a bit of memory.. Is there another way for checking the phone number format validation without libphonenumber.jar??? can you suggests something???

like image 249
Preeti Avatar asked Sep 10 '14 11:09

Preeti


People also ask

How do you check if a mobile number is valid or not in Java?

Mobile number validation in Java is done using Pattern and Matcher classes of Java. The pattern class is used to compile the given pattern/regular expression and the matcher class is used to match the input string with compiled pattern/regular expression.

What is an E 164 phone number?

E. 164 is the international telephone numbering plan that ensures each device on the PSTN has globally unique number. This number allows phone calls and text messages can be correctly routed to individual phones in different countries.


1 Answers

This answer might help you: https://stackoverflow.com/a/5959341

To validate a string, use

if (setNum.matches(regexStr))
where regexStr can be:

//matches numbers only
String regexStr = "^[0-9]*$"

//matches 10-digit numbers only
String regexStr = "^[0-9]{10}$"

//matches numbers and dashes, any order really.
String regexStr = "^[0-9\\-]*$"

//matches 9999999999, 1-999-999-9999 and 999-999-9999
String regexStr = "^(1\\-)?[0-9]{3}\\-?[0-9]{3}\\-?[0-9]{4}$" 

There's a very long regex to validate phones in the US (7 to 10 digits, extensions allowed, etc.). The source is from this answer: A comprehensive regex for phone number validation

String regexStr = "^(?:(?:\\+?1\\s*(?:[.-]\\s*)?)?(?:\\(\\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\\s*\\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\\s*(?:[.-]\\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\\s*(?:[.-]\\s*)?([0-9]{4})(?:\\s*(?:#|x\\.?|ext\\.?|extension)\\s*(\\d+))?$"
like image 53
QArea Avatar answered Oct 05 '22 23:10

QArea