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,917
  • 7
  • 38
  • 56

2 Answers2

24

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

PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
konakid
  • 656
  • 5
  • 7
1

This was the regular expression that fixed the issue:

(\\+[0-9]+[\\- \\.]*)?" + "(\\([0-9]+\\)[\\- \\.]*)?" + "([0-9][0-9\\- \\.]+[0-9])
user268397
  • 1,917
  • 7
  • 38
  • 56