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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:26:39+00:00 2026-05-31T13:26:39+00:00

I’m working on a set of validation classes and am currently building plugins for

  • 0

I’m working on a set of validation classes and am currently building plugins for applying various validation rules. I’ve built the following class for validating a UK postcode:

class PostcodeUk extends abstr\Prop implements iface\Prop
{
    const 

        /**
         * Defines the regular expression against which to test postal code
         * 
         * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation UK postal code validation rules on Wikipedia 
         */
        PATTERN = '/^(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2})$/';

    /**
     *
     * @return bool True if valid
     * @throws \InvalidArgumentException 
     */
    public function isValid ()
    {
        $valid  = false;
        $data   = $this -> getData ();

        switch (gettype ($data))
        {
            case 'NULL'     :
                $valid  = true;
            break;
            case 'string'   :
                $valid  = preg_match (static::PATTERN, $data) > 0;
            break;
            default         :
                throw new \InvalidArgumentException (__CLASS__ . ': This property cannot be applied to data of type ' . gettype ($data));
            break;
        }

        return ($valid);
    }
}

The regex defined in PostcodeUk::PATTERN was derived from the on given in Wikipedia’s article on UK postcodes. However, the regex as given detects valid postcode strings contained within bigger blocks of text. I want it to exactly match a valid post code only, excluding preceding and following characters. So (SW1A 0AA) should be passed as valid, but (foobarSW1A 0AA) should not.

I added the anchors to the regex (^ at the start and $ at the end) to try and force it to only accept a string that consists of only a postcode as valid. However, the class still passes postcodes with padding and/or non-postcode strings wrapping it.

What am I doing wrong? I thought adding the anchors would be enough to get the behaviour I wanted.

  • 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-05-31T13:26:40+00:00Added an answer on May 31, 2026 at 1:26 pm

    Add the anchors as:

    ^(?:regex)$
    

    ^foo|bar$ is not the same as ^(?:foo|bar)$.

    You should also use \z instead of $. $ allows an optional line break at the end of string, while \z is a strict end of string match.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.