1

I am allowing user to register with mobile number only and i wanna validate number in app without using internet, what i wanna do is that get valid phone number length of country selected by user , validate it with length and formatting of number in that country's format.

for e.g. length of India's phone number is 10 and format is 0**********

Please tell me how to do that.

Shivam Nagpal
  • 763
  • 2
  • 9
  • 21
  • What language of code are you using? Are you just looking to enforce a character limit / minimum in a text field? – Vandal Oct 16 '15 at 09:19
  • @Spyder_Says_hi I am writing code for android app so it supports java. with length of phone that country's number i wanna validate weather user has entered correct number or not – Shivam Nagpal Oct 16 '15 at 09:22

4 Answers4

6

Add Following Code.

In gradle

 compile 'com.googlecode.libphonenumber:libphonenumber:8.5.2'

In Activity class

PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(countryCode));
    String  exampleNumber= String.valueOf(phoneNumberUtil.getExampleNumber(isoCode).getNationalNumber());
    int phoneLength=exampleNumber.length();
    editTextLoginPhone.setFilters( new InputFilter[] {
            new InputFilter.LengthFilter(phoneLength)});

Hope this code will help you.

Guru raj
  • 806
  • 11
  • 10
  • @dennisrufigill prefixes code for respective country phone numbers(like 1 for US, 91 for India, 44 for UK) refer https://countrycode.org/ – Guru raj Sep 29 '20 at 14:31
1

You can try this code for Indian Phone numbers:-

private static boolean isValidPhoneNumber(String mobile) {
    String regEx = "^[0-9]{10}$";
    return mobile.matches(regEx);
}
sherlock
  • 97
  • 5
Anand Jain
  • 2,365
  • 7
  • 40
  • 66
  • There are some countries who's phone number length is less than 10 or greater than 10 that's why i need length of valid phone number for the country selected by user – Shivam Nagpal Oct 16 '15 at 09:27
1

if you not want to use the internet of checking the number valid or not , you can do programmatically though its long but solution can be this only . Go to link : https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country

and do programmatically the validation with conditions.

Shivani Gupta
  • 271
  • 3
  • 12
0

you should force user to enter the country code and you can validate the phone number also just go through the links below

https://github.com/googlei18n/libphonenumber and List of phone number country codes

Community
  • 1
  • 1
Maniya Joe
  • 761
  • 6
  • 24