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

  • Home
  • SEARCH
  • 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 8431343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:49:46+00:00 2026-06-10T05:49:46+00:00

i have this function to check my phone number: function isValid( $what, $data )

  • 0

i have this function to check my phone number:

function isValid( $what, $data ) {

        switch( $what ) {

                // validate a phone number
                case 'phone_number':
                        $pattern = "/^[0-9-+]+$/";

                break;



                default:
                        return false;
                break;

        }
        return preg_match($pattern, $data) ? true : false;
}

i want to change that regex to accept the following: the ) ( chars like (800) and the space.

So for example this number will pass the validation, right now is not passing:

+1 (201) 223-3213

  • 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-10T05:49:47+00:00Added an answer on June 10, 2026 at 5:49 am

    Let us construct the regular expression step by step. Consider also that spaces are trimmed before matching.

    • at the beginning there might or might not be a + sign. This also needs to be escaped. \+?
    • then comes one or more digits, before the part with parenthesis [0-9]+ You might want to write [0-9]* if the number can begin directly with a group in parenthesis
    • then, optionally comes a group of digits in parenthesis: (\[0-9]+\)?. Suppose that only one such group is allowed
    • then comes the local phone number, hyphens also allowed: [0-9-]*
    • the final character must be a digit [0-9], hyphen is not allowed here

      ^\+?[0-9]+(\([0-9]+\))?[0-9-]*[0-9]$
      

    See the result here. Trimming spaces looks like $trimmed = str_replace(' ', '', $pattern);.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function: function validate($data) { $newData = str_replace( , , $newData); $newData =
I have created this function to check abecedarian with while loop (A word is
I have used Javascript onlaod like this: function check() { var pic = new
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have the next code: js: $('[id^=check-]').change(function(){ var new_id = this.id.replace(/check/, 'new'), real_id =
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have this function // add history paths and save data function AddPath( strTag,
I have this function coded to check the status of a Rapidshare link using
I have this security question answer input field validate function where I want to
I have this code snippet inside a function that checks if an object exists

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.