I'm trying to come up with a regexp that can be used to validate Canadian and United States phone number. Now in Canada, it is required to use a 3 digit area code, then the actual phone number. From what I know it's not required in the United States to do that.
Well I came up with:
PHP Code:
if (preg_match("/[0-9]{3}?-[0-9]{3}-[0-9]{4}/", "555-5555")) {
echo "Valid phone number.";
}
that matches any 3 digit code, then another 3 digit code, then any 4 digit codes. Such as 555-555-5555 but if the phone number being validated doesn't use an area code then it will fail because it doesn't match. If the user provides
555-5555 it will match the first part [0-9]{3}? but then will fail the next part of the regexp. Any ideas to correct this?