Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone number validation Android

How do I check if a phone number is valid or not? It is up to length 13 (including character + in front).

How do I do that?

I tried this:

String regexStr = "^[0-9]$";  String number=entered_number.getText().toString();    if(entered_number.getText().toString().length()<10 || number.length()>13 || number.matches(regexStr)==false  ) {     Toast.makeText(MyDialog.this,"Please enter "+"\n"+" valid phone number",Toast.LENGTH_SHORT).show();     // am_checked=0; }` 

And I also tried this:

public boolean isValidPhoneNumber(String number) {      for (char c : number.toCharArray())      {          if (!VALID_CHARS.contains(c))          {             return false;          }      }      // All characters were valid      return true; } 

Both are not working.

Input type: + sign to be accepted and from 0-9 numbers and length b/w 10-13 and should not accept other characters

like image 520
Udaykiran Avatar asked Jun 15 '11 13:06

Udaykiran


People also ask

How can I validate my phone number?

Mobile Number validation criteria:The first digit should contain number between 7 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.

Can I check if a phone number is available?

Visit www.textmagic.com or download the TextMagic mobile app from the Google Play store. Enter your phone number and country, then click Validate Number. This app will tell you whether the phone number is active or not. Phone Number Monitoring is another app that you can use.

Is there a free way to verify a phone number?

AnyWho is an online service that lets you reverse lookup and identify phone numbers for free. Just enter the phone number you're looking for and it'll display all the details about it.


2 Answers

Use isGlobalPhoneNumber() method of PhoneNumberUtils to detect whether a number is valid phone number or not.

Example

System.out.println("....g1..."+PhoneNumberUtils.isGlobalPhoneNumber("+912012185234")); System.out.println("....g2..."+PhoneNumberUtils.isGlobalPhoneNumber("120121852f4")); 

The result of first print statement is true while the result of second is false because the second phone number contains f.

like image 62
Sunil Kumar Sahoo Avatar answered Sep 28 '22 07:09

Sunil Kumar Sahoo


Given the rules you specified:

upto length 13 and including character + infront.

(and also incorporating the min length of 10 in your code)

You're going to want a regex that looks like this:

^\+[0-9]{10,13}$ 

With the min and max lengths encoded in the regex, you can drop those conditions from your if() block.

Off topic: I'd suggest that a range of 10 - 13 is too limiting for an international phone number field; you're almost certain to find valid numbers that are both longer and shorter than this. I'd suggest a range of 8 - 20 to be safe.

[EDIT] OP states the above regex doesn't work due to the escape sequence. Not sure why, but an alternative would be:

^[+][0-9]{10,13}$ 

[EDIT 2] OP now adds that the + sign should be optional. In this case, the regex needs a question mark after the +, so the example above would now look like this:

^[+]?[0-9]{10,13}$ 
like image 33
Spudley Avatar answered Sep 28 '22 07:09

Spudley