Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:14:42+00:00 2026-06-10T16:14:42+00:00

I have a phone number field that is set up to validate using preg_match:

  • 0

I have a phone number field that is set up to validate using preg_match:

function ValidPhone($phone) {
    return preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', trim($phone));
}

This ensures that phone numbers are entered in the xxx-xxx-xxxx format and that it’s numeric.

I’m not very familiar with regexp; can anyone tell me how I’d expand this to allow more than one format? i.e. xxx xxx xxxx or xxx.xxx.xxxx or (xxx) xxx-xxxx

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:14:44+00:00Added an answer on June 10, 2026 at 4:14 pm

    I think the easiest solution would be to just strip out everything that isn’t numeric first:

    function ValidatePhone($phone) {
        $phone = preg_replace('/\D+/', '', $phone);
        return strlen($phone) == 10;
    }
    

    Since you are validating the format, and not the content, this should do it:

    function ValidatePhone($phone) {
        $patterns = array(
            '\d{3}([-\. ])\d{3}\g{-1}\d{4}',
            '\(\d{3}\) \d{3}-\d{4}'
            );
    
        $phone = trim($phone);
    
        foreach ($patterns as $pattern) {
            if (preg_match('/^' . $pattern . '$/', $phone))
                return true;
        }
    
        return false;                                                                                                                   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a phone number field that can have lots of different characters such
I have a form with phone number field, i need to validate it with
I have a form that contains a phone number field <input name=phNumber type=text class=numbersonly/>
I'd like to have a phone number text field pre-formatted for a 10 digit
If I have an international phone number such as this: 0541754301 how can I
I have a data set that looks like the following. EMPLID PHONE_TYPE PHONE ------
I'm trying to validate my form. In my form is a phone number field.
I have few text fields like SSN , phone number and email id. I
I have phone number. Is there any way to check whether the phone number
I have a phone number I want to match against a regular expression. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.