On 17 February 2004 19:44, Shaun contributed these pearls of wisdom:

> However the following code wont let me add any postcode!:
> 
> if(!preg_match("/[A-Z]{1,2}[0-9][0-9A-Z]?
> [0-9][ABD-HJLNP-UW-Z]{1,2}/",
> $_POST[postcode])){
>    $error = "Invalid Postcode";
>    header("Location: add_location.php?error=$error");
>    exit;
> }
> 
> Any ideas?

Well, I just cut'n'pasted the above and tested it and it works fine for me.  The only 
things I can think of are fairly obvious ones that I'm sure you've already thought of:

* the postcode needs to be in upper case to match that regex -- if you want to accept 
lower-case input, it should be upper-cased before matching.
* have you tried echoing $_POST['postcode'] to make sure it contains what you think it 
should?

Also, I need to correct a minor error in the regexp; it should have read:

  "/[A-Z]{1,2}[0-9][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/"

Also also, two other points points made in other solutions: you should probably add 
allowance for leading/trailing spaces, and for additional whitespace between the two 
parts of the code; and, as currently written, that regexp matches if there is a valid 
postcode anywhere in the input, so it should be anchored to the beginning and end of 
the string.  Both of these can be catered for in the regexp (rather than with extra 
PHP code), resulting in:

  "/^\\s*[A-Z]{1,2}[0-9][0-9A-Z]?\\s+[0-9][ABD-HJLNP-UW-Z]{2}\\s*$/"

Cheers!

Mike

-- 
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to