Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Phone Number - Android [duplicate]

Tags:

regex

android

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?

like image 329
user268397 Avatar asked Nov 28 '22 01:11

user268397


2 Answers

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

PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)

like image 196
konakid Avatar answered Dec 17 '22 19:12

konakid


This was the regular expression that fixed the issue:

(\\+[0-9]+[\\- \\.]*)?" + "(\\([0-9]+\\)[\\- \\.]*)?" + "([0-9][0-9\\- \\.]+[0-9])
like image 28
user268397 Avatar answered Dec 17 '22 18:12

user268397