Regular expression for validating phone number
The literal “x” character is required only if an extension is provided. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
The last question mark is to make country code optional. The leading plus sign and the dot following the country code are required.
Different countries have different number formats, so it's less straightforward to parse.
If you wish to add support for a specific country like in your example, you can write a pattern for it as well.
There are too many regex variants to validate a phone number. \d )$/ Here is the Java Script function This validates the following formats: 44 07988-825 465 (with any combination of hyphens in place of space except that only a space must follow the 44) 44 (0) 7988-825 465 (with any combination of hyphens in place of spaces except that no hyphen can exist directly before or after the (0) and the space before or after the (0) need not exist) 123 456-789 0123 (with any combination of hyphens in place of the spaces) 123-123 123 (with any combination of hyphens in place of the spaces) 123 123456 (Space can be replaced with a hyphen) 1234567890 No double spaces or double hyphens can exist for all formats.
I recommend this article: Java Script: HTML Form - Phone Number validation, where multiple variants are specified for each case.
The second format is more tricky, since it is an international phone number. If you want to match at word boundaries, just change the ^ and $ to \b I welcome any suggestions, corrections, or criticisms of this solution.As far as I can tell, this matches the NANP format (for USA numbers - I didn't validate other North American countries when creating this), avoids any 911 errors (can't be in the area code or region code), eliminates only those 555 numbers which are actually invalid (region code of 555 followed by 01xx where x = any number). where C is the 1–3 digit country code, N is up to 14 digits, and E is the (optional) extension. Also this will return the Country, Area and Provider demo https://libphonenumber.appspot.com/ git https://github.com/googlei18n/libphonenumber/releases/tag/v8.9.0 The format CCC. NNNNNNNNNNx EEEE Phone number must start with ' ' or '00' for an international call.
Search for regular expression for validating phone number:
If you want to deepen in regex for a custom expression, you can review this documentation.