Reputation: 763
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.
Upvotes: 1
Views: 5268
Reputation: 826
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.
Upvotes: 7
Reputation: 2385
You can try this code for Indian Phone numbers:-
private static boolean isValidPhoneNumber(String mobile) {
String regEx = "^[0-9]{10}$";
return mobile.matches(regEx);
}
Upvotes: 1
Reputation: 271
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.
Upvotes: 1
Reputation: 801
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
Upvotes: 0