Tlaloc-ES
Tlaloc-ES

Reputation: 5282

Is there a pattern to validate if a phone number is of a concrete country?

I want to know if there is any default pattern to validate if a phone number is of a concrete country?

Currently, I am using a Patterns.PHONE.matcher(phone).matches(); but this validates a phone, no an EEUU phone, Italy phone, etc...

How can do for know if the telephone number is a number of a concrete country? And is it possible to do without the international prefix like +0012?

Upvotes: 1

Views: 370

Answers (1)

Kishore Jethava
Kishore Jethava

Reputation: 6834

Use libphonenumber library from Google

implementation 'io.michaelrocks:libphonenumber-android:8.8.5'

code

  PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.createInstance(this)
  PhoneNumber phoneNumber = phoneNumberUtil.parse("+917874962727", null)
  String regionCode = phoneNumberUtil.getRegionCodeForNumber(phoneNumber) // IN for India

getRegionCodeForNumber will give you country code. further, you can map like

  • US => USA
  • IN => India
  • IT => Italy

Upvotes: 2

Related Questions