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 8124495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:28:26+00:00 2026-06-06T06:28:26+00:00

Bear with me, as I’m quite new to using regular expressions. I have regexp

  • 0

Bear with me, as I’m quite new to using regular expressions.

I have regexp checks on three fields, as marked in my code below. No matter what I enter in the fields, neither my character length restrictions, nor my actual character restrictions are enforced; all of these fields seem to simply “skip over” my regular expression checks and validate as true.

// NAME
    if (preg_match('%^[A-Za-z\.\' \-]{12,80}$%', stripslashes($name)) === false) {
        $name = false;
        $errors[] = 'You need to enter a valid name. No slashes or quotes are allowed.';
    }
    else { echo 'Test: RegExp Validation passed for "NAME" <br />'; }

// PHONE NUMBER
    if (preg_match('%^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$%', $phone) === false) {
        $phone = false;
        $errors[] = 'A valid phone number must be entered. Only numbers, dashes, and parentheses are allowed';
    }
    else { echo 'Test: RegExp Validation passed for "PHONE" <br />'; }

//EXTENSION
    if (!empty($phnext) && preg_match('%^[0-9]{3,5}$%', $phnext) === false) {
        $phnext = false;
        $errors[] = 'Only 3 to 5 numbers are allowed for the phone extension.';
    }
    else { echo 'Test: RegExp Validation passed for "EXTENSION" <br />'; }

I don’t get any syntax, compilation, or out-of-range errors. So I’m wondering, are my actual regexp strings wrong, or is my logic wrong somewhere? It wouldn’t be the first time. I’m just a little stumped.

  • 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-06T06:28:27+00:00Added an answer on June 6, 2026 at 6:28 am

    preg_match will only return FALSE if an error occurred. In the case of no matches, it will return 0. You should not be using a strict test preg_match(...) === FALSE, instead you can use:

    if (preg_match(...)) {
        // passed
    } else {
        // didn't pass
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bear with me, I'm new to manipulating XML using jQuery. Say I have an
Bear with me as I am new to obj-c but I have a UIScrollView
Please bear with me on this as I'm new to this. I have an
Bear with me if this is unclear; I have trouble fully wrapping my head
bear with me, I've been using wxPython for all of one day now. Short
Please bear with me as I am new to dojo, javascript, and web programming
Please bear in mind that I'm totally new to Rails when answering this.My question
Bear with me. I have not coded in c in 8 years and am
Bear with me, I have been only learning PHP for only a few weeks,
Bear with me, I know access_token questions have been asked a thousand times but

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.