www.webdeveloper.com
Results 1 to 4 of 4

Thread: validation..again!

  1. #1
    Join Date
    Apr 2005
    Posts
    15

    validation..again!

    having problems getting the postcode field to to only allow if first and last character is a letter. im tryin 2 use the following...any sugestions?

    // Returns true if a letter is found at the beginning of $postalcode.
    ereg("^abcdefghijklmnopqrstuvwxyz", $postalcode);

    // Returns true if a letter is found at the end of $postalcode.
    ereg("abcdefghijklmnopqrstuvwxyz$", $postalcode);

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    And how does a postal code look?

  3. #3
    Join Date
    Apr 2005
    Posts
    15
    postal code should always have a letter at the start and end e.g NE7 7BB. With a space between

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by mastap
    postal code should always have a letter at the start and end e.g NE7 7BB. With a space between
    The UK postal code system is not that simple. The format of UK postcodes is generally:
    • LD DLL
    • LLD DLL
    • LDD DLL
    • LLDD DLL
    • LLDL DLL
    • LDL DLL
    • where L signifies a letter and D a digit.

    There is only one UK mainland postcode that breaks the above syntax rules: GIR 0AA which is the postcode of the, formerly Post Office owned, National Girobank.

    You should not check for the space because some people will omit it. The following will check the postcode complies with that syntax and if it does it will format it upper case with the space ready to use:
    PHP Code:
    <?php

    $postcode 
    'SW1A 2AA'# 10 downing street

    if((preg_match('@^([A-Z]{1,2}[0-9]{1,2}[a-z]?) ?([0-9][ABDEFGHJLNPQRSTUWXYZ]{2})$@i'$postcode$matches)) and ($postcode strtoupper($matches[1].' '.$matches[2])))
    {
        
    # valid
    }
    else
    {
        
    # invalid
    }

    ?>
    Last edited by bokeh; 04-22-2006 at 10:08 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles