Home > Web Front-end > JS Tutorial > How Can I Validate Phone Numbers with Consecutive Digits Using JavaScript?

How Can I Validate Phone Numbers with Consecutive Digits Using JavaScript?

Patricia Arquette
Release: 2024-12-02 20:43:11
Original
681 people have browsed it

How Can I Validate Phone Numbers with Consecutive Digits Using JavaScript?

Validate Phone Numbers with Consecutive Digits in JavaScript

The original regular expression provided aims to validate phone numbers in two specific formats: (123) 456-7890 and 123-456-7890. However, the client now requires an additional validation method for phone numbers with consecutive digits, such as 1234567890.

To accommodate this new requirement, we can extend the regular expression to include the desired format:

/^(?:\+?[\d]{1,3}\W?)?(?:[(]\d{3}[)]|\d{3})?(?:-|\s)?(?:\d{3}-|\d{3}\s)?\d{4,6}$/im
Copy after login

Breakdown of the Regex:

  • ^: Start of the string.
  • (?: [d]{1,3}W?)?: Optional international calling code with up to three digits.
  • (?:[(]d{3}[)]|d{3})?: Area code enclosed in parentheses or as a three-digit group.
  • (?:-|s)?: Optional hyphen or whitespace between the area code and local number.
  • (?:d{3}-|d{3}s)?: Exchange prefix with optional hyphen or whitespace.
  • d{4,6}$: Local number with a length between 4 and 6 digits.
  • im: Ignore case and multiline (allows line breaks within the phone number).

Example Valid Phone Numbers:

  • (123) 456-7890
  • 123-456-7890
  • 1234567890
  • 1 (415)-555-1212
  • 1 123-456-7890
  • 075-63546725
  • 31636363634

The above is the detailed content of How Can I Validate Phone Numbers with Consecutive Digits Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template