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?
Instead of making your own regexp, you can use Android's built in method
PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)
This was the regular expression that fixed the issue:
(\\+[0-9]+[\\- \\.]*)?" + "(\\([0-9]+\\)[\\- \\.]*)?" + "([0-9][0-9\\- \\.]+[0-9])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With