4

I'm trying to create a regular expression to check to see if a valid phone number has been entered. There is something wrong with my regular expression. Here is the source code I'm using:

if (!Pattern.matches("(\\d{3}-){1,2}\\d{4}", s)) {
                    et.setError("Enter a valid Phone Number");
}

What am I doing wrong?

user268397
  • 1,897
  • 7
  • 36
  • 54

2 Answers2

24

Instead of making your own regexp, you can use Android's built in method

PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)

Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
konakid
  • 603
  • 5
  • 7
1

This was the regular expression that fixed the issue:

(\\+[0-9]+[\\- \\.]*)?" + "(\\([0-9]+\\)[\\- \\.]*)?" + "([0-9][0-9\\- \\.]+[0-9])
user268397
  • 1,897
  • 7
  • 36
  • 54