Recently I was tasked with writing a regular expression that would check for a valid Australian phone number for both landline and mobile phone variants whilst allowing for different formats (spaces, no spaces, international dialing code, brackets, area code, no area code).
The below regular expression code and subsequent tests are for Australian mobile and landline numbers only, but could be tweaked to work for other countries as well. phone Number7.match(phone Expression)) Supported formats: 0411 234 567, 61411 234 567, (02) 3892 1111, 38921111, 3892 111, 02 3892 1111 I haven’t encountered any issues using this expression, but it is possible there could be caveats using this.
I consider the following to be valid phone number formats: var p = "5558675309"; var p = "(312) 123-4567"; var p = "512-234-5678"; Thanks, - Dave Hi, Does anyone have a cross-browser JS function that validates a US phonenumber?
I consider the following to be valid phone number formats:var p = "5558675309";var p = "(312) 123-4567";var p = "512-234-5678"; function validate USAian Phone(str) This function is pretty liberal, accepting any string containing exactly 10 or 11 digits. It still won't help you with the people who still think in terms of named exchanges, such as KL5-9991, but they're all pretty old by now.
Formerly, area codes were required and exchanges were prohibited to have0 or 1 as the 2nd digit, but that restriction was eliminated a few yearsago with the advent of 10-digit local dialing.
This regular expression matches three groups of digits. ( # Capture the enclosed match to backreference 1... is another character class, one that allows any one of three separators.
, composing a fairly comprehensive guide to validating and formatting North American and international phone numbers using regular expressions. Thus, the sequences that enclose the first group of digits match literal parenthesis characters.
The regexes in these recipes are all pretty straightforward, but hopefully this gives an example of the depth you can expect from the book. Both are followed by a question mark, which makes them optional.
⋯ # [Match the remaining digits and separator.] $ # Assert position at the end of the string..
Since a question mark is used after each separator, the phone number digits are allowed to run together.