check phone number with regex - Search
Open links in new tab
  1. 12

    Validating phone numbers is crucial to ensure data consistency and integrity in applications. Regular expressions (regex) provide a powerful tool for pattern matching and validation. Here, we'll explore how to use regex to validate phone numbers, including those with country codes.

    Regex Pattern for Phone Number Validation

    To validate phone numbers, we need a regex pattern that checks for the following:

    • The number should start with a plus sign (+).

    • It should be followed by the country code and national number.

    • It may contain white spaces or hyphens (-).

    • The length of the phone number may vary from 7 to 15 digits.

    Here is a regex pattern that meets these criteria:

    ^\+[1-9]{1}[0-9]{0,2}[-\s]?[2-9]{1}[0-9]{2}[-\s]?[2-9]{1}[0-9]{2}[-\s]?[0-9]{4}$

    Explanation:

    • ^: Asserts the start of the string.

    • \+: Matches the plus sign.

    • [1-9]{1}[0-9]{0,2}: Matches the country code (1 to 3 digits, not starting with 0).

    • [-\s]?: Matches an optional hyphen or whitespace.

    • [2-9]{1}[0-9]{2}: Matches the area code (3 digits, not starting with 0 or 1).

    • [-\s]?: Matches an optional hyphen or whitespace.

    • [2-9]{1}[0-9]{2}: Matches the telephone prefix (3 digits, not starting with 0 or 1).

    • [-\s]?: Matches an optional hyphen or whitespace.

    • [0-9]{4}: Matches the line number (4 digits).

    • $: Asserts the end of the string.

    Continue reading
    Feedback
    Kizdar net | Kizdar net | Кыздар Нет
  1. Some results have been removed