/    Sign up×
Community /Pin to ProfileBookmark

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);

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 22.2006 — And how does a postal code look?
Copy linkTweet thisAlerts:
@mastapauthorApr 22.2006 — postal code should always have a letter at the start and end e.g NE7 7BB. With a space between
Copy linkTweet thisAlerts:
@bokehApr 22.2006 — postal code should always have a letter at the start and end e.g NE7 7BB. With a space between[/QUOTE]The UK postal code system is not that simple. The format of UK postcodes is generally:[list]
  • [*]LD DLL

  • [*]LLD DLL

  • [*]LDD DLL

  • [*]LLDD DLL

  • [*]LLDL DLL

  • [*]LDL DLL

  • [*]where L signifies a letter and D a digit.

  • [/list]

    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:[code=php]<?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
    }

    ?>[/code]
    ×

    Success!

    Help @mastap spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.4,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...