Click to See Complete Forum and Search --> : Updating a Postcode Validation Code


peteyb
02-17-2006, 05:55 AM
I am using the following code to validate my post code field and it is working great.


function checkPostcode (&$toCheck) {

// Permitted letters depend upon their position in the postcode.
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
$alpha3 = "[abcdefghjkstuw]"; // Character 3
$alpha4 = "[abehmnprvwxy]"; // Character 4
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5

// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
$pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';

// Expression for postcodes: ANA NAA
$pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';

// Expression for postcodes: AANA NAA
$pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';

// Exception for the special postcode GIR 0AA
$pcexp[3] = '^(gir)(0aa)$';

// Standard BFPO numbers
$pcexp[4] = '^(bfpo)([0-9]{1,4})$';

// c/o BFPO numbers
$pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';

// Load up the string to check, converting into lowercase and removing spaces
$postcode = strtolower($toCheck);
$postcode = str_replace (' ', '', $postcode);

// Assume we are not going to find a valid postcode
$valid = false;

// Check the string against the six types of postcodes
foreach ($pcexp as $regexp) {

if (ereg($regexp,$postcode, $matches)) {

// Load new postcode back into the form element
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);

// Take account of the special BFPO c/o format
$toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);

// Remember that we have found that the code is valid and break from loop
$valid = true;
break;
}
}

// Return with the reformatted valid postcode in uppercase if the postcode was
// valid
if ($valid){return true;} else {return false;};
}
?>


How can i change it to block any postcodes that do not begin with EX, TQ, TR, PL and also 20 individual full post codes (e.g pl20 6ba)?

bokeh
02-17-2006, 08:28 AM
For what country are those postal codes?

peteyb
02-17-2006, 09:30 AM
uk

bokeh
02-17-2006, 10:06 AM
Without interfering with the rest of the function, which looks pretty inefficient to me, I have made some minor changes at the start (adding a banned array) and a change at the end which now returns immediately rather than mucking about breaking the loop. function checkPostcode (&$toCheck)
{
$not_allowed = array('EX', 'TQ', 'TR', 'PL', 'pl20 6ba');
if(eregi('^('.implode('|', $not_allowed).')', $toCheck)) return false;
// Permitted letters depend upon their position in the postcode.
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
$alpha3 = "[abcdefghjkstuw]"; // Character 3
$alpha4 = "[abehmnprvwxy]"; // Character 4
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5

// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
$pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';

// Expression for postcodes: ANA NAA
$pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';

// Expression for postcodes: AANA NAA
$pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';

// Exception for the special postcode GIR 0AA
$pcexp[3] = '^(gir)(0aa)$';

// Standard BFPO numbers
$pcexp[4] = '^(bfpo)([0-9]{1,4})$';

// c/o BFPO numbers
$pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';

// Load up the string to check, converting into lowercase and removing spaces
$postcode = strtolower($toCheck);
$postcode = str_replace (' ', '', $postcode);

// Check the string against the six types of postcodes
foreach ($pcexp as $regexp) {

if (ereg($regexp,$postcode, $matches)) {

// Load new postcode back into the form element
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);

// Take account of the special BFPO c/o format
$toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);

// valid postal code, return true
return true;
}
}
// invalid postal code, return false
return false;
}

peteyb
02-18-2006, 04:09 AM
I have inserted my 500 specified postcodes that are not allowed pass the validation into the $not_allowed command but it appears to be throwing errors with some of them.

For example, EX37 9AB is on the list and as expected throws the error text but EX37 9AC is not on the list but also throws an error. To add, EX37 9AD (not on the list) does not throw an error. Why would this be?

Another anomoly, is if i type EX10 OLJ (which is on the list) the error is not throw but if the copy and paste it from my code it throws the expected error.

Why are these anomolies occuring?

bokeh
02-18-2006, 04:22 AM
For example, EX37 9AB is on the list and as expected throws the error text but EX37 9AC is not on the list but also throws an error. To add, EX37 9AD (not on the list) does not throw an error. Why would this be?In the code posted above all codes starting EX are on the list.

I'd be happy to look at this if you post the $not_allowed array and a list of the errors occuring.

bokeh
02-18-2006, 04:26 AM
How can i change it to block any postcodes that do not begin with EX, TQ, TR, PL and also 20 individual full post codes (e.g pl20 6ba)?Here's the thing, I think I misread the question. The above postal codes are the ones you want to allow, correct? I read it that you want to block these codes. Which is it?

peteyb
02-18-2006, 04:33 AM
I gathered you read it wrong but i changed the code accordingly.

What i mean is that i

a) only want to ALLOW uk postcode that start with EX, PL, TQ and TR

b) but have 500 specific full postcodes that fall into this group must be stopped


I have taken your $not_allowed command and added the 500 specified postcodes and the first characters of all other uk postcodes.

Hopefully my aim is now clearer.

Do you have any suggestions to the questions raised in my previous thread?

or

You said my code

which looks pretty inefficient to me

Do you know of a better code as im open to any suggestions.

bokeh
02-18-2006, 04:57 AM
I modified it slightly and now you have $banned and $permitted arrays. $banned overrides $permitted. EX37 9AC will never work though because C isn't resident in $alpha5.

function checkPostcode (&$toCheck)
{
$permitted = array('EX', 'TQ', 'TR', 'PL');
if(!eregi('^('.implode('|', $permitted).')', $toCheck)) return false;
$banned = array('pl20 6ba');
if(eregi('^('.implode('|', $banned).')', $toCheck)) return false;
// Permitted letters depend upon their position in the postcode.
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
$alpha3 = "[abcdefghjkstuw]"; // Character 3
$alpha4 = "[abehmnprvwxy]"; // Character 4
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5

// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
$pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';

// Expression for postcodes: ANA NAA
$pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';

// Expression for postcodes: AANA NAA
$pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';

// Exception for the special postcode GIR 0AA
$pcexp[3] = '^(gir)(0aa)$';

// Standard BFPO numbers
$pcexp[4] = '^(bfpo)([0-9]{1,4})$';

// c/o BFPO numbers
$pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';

// Load up the string to check, converting into lowercase and removing spaces
$postcode = strtolower($toCheck);
$postcode = str_replace (' ', '', $postcode);

// Check the string against the six types of postcodes
foreach ($pcexp as $regexp) {

if (eregi($regexp,$postcode, $matches)) {

// Load new postcode back into the form element
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);

// Take account of the special BFPO c/o format
$toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);

// valid postal code, return true
return true;
}
}
// invalid postal code, return false
return false;
}

peteyb
02-18-2006, 05:20 AM
Legend.

This code now makes a millions time more sense.

I have updated the alphas as follows


$alpha1 = "[etp]"; // Character 1
$alpha2 = "[lqrx]"; // Character 2
$alpha3 = "[abcdefghijklmnopqrstuwxyz]"; // Character 3
$alpha4 = "[abcdefghijklmnopqrstuwxyz]"; // Character 4
$alpha5 = "[abcdefghijklmnopqrstuwxyz]"; // Character 5

peteyb
02-18-2006, 05:37 AM
Whilst this thread is alive and kicking i wander if it were possible to change the error message displayed depending on where the user failed the validation.

I am using:

if (!checkPostcode ($postcode) )
{
$error_postcode = "<tr><td>&nbsp;</td><td class=\"error\">Invalid postcode</td></tr>";
$errors[] = '&nbsp';
}
else
{
$postcode = escape_data($_POST['postcode']);
}


If the user fails because they are in the lost of 500 postcodes i would rather the message informed them of that, but if they simply mistyped a postcode i would prefer the current message.

any ideas?

bokeh
02-18-2006, 06:06 AM
To me it seems like you have a huge amount of padding for no good reason. Something as simple as the following should work fine:function checkPostcode (&$postcode)
{
$banned = array('pl20 6ba');
$allowed = '/^(?!'.implode('|', $banned).')((?:ex|pl|tq|tr)[0-9]{1,2})\s?([0-9]{1}[a-z]{2})$/i';
if(!preg_match($allowed, $postcode, $matches)) return false;
$postcode = strtoupper ($matches[1] . ' ' . $matches [2]);
return true;
}

bokeh
02-18-2006, 06:14 AM
Also your method with $alpha1 and $alpha2 is wrong as it will let through combinations such as EL and PQ.

peteyb
02-19-2006, 09:35 AM
Have implemented your code and it throws up this error

Warning: implode(): Bad arguments. in /home/t/i/tigltd/public_html/quote/functions/phppostcode.php on line 38

bokeh
02-19-2006, 09:43 AM
It works for me. Maybe there is something wrong with your banned array. Post the banned array.

peteyb
02-19-2006, 09:54 AM
i still had my $banned as $not_allowed.

I have changed that but now the code is not letting any postcodes through.

my banned list is:


$banned = array('EX10 0AF', 'EX10 0JX', 'EX10 0LB', 'EX10 OLD', 'EX10 OLJ', 'EX10 OLW', 'EX10 OLD', 'EX10 ONG', 'EX10 OSF', 'EX10 9PN', 'EX10 OLD', 'EX11 1LU', 'EX11 1NF', 'EX12 4AF', 'EX13 5RS', 'EX13 7LF', 'EX13 7LN', 'EX13 7LW', 'EX13 7NN', 'EX13 7PE', 'EX13 7PG', 'EX13 7PJ', 'EX13 7PN', 'EX13 7PP', 'EX13 7PQ', 'EX13 7RG', 'EX14 3HE', 'EX14 3NZ', 'EX15 1BH', 'EX15 1BS', 'EX15 1BW', 'EX15 1NX', 'EX15 1PA', 'EX15 1QL', 'EX15 1XL', 'EX15 2LQ', 'EX15 2ND', 'EX15 2NW', 'EX15 2RB', 'EX15 3AR', 'EX15 3DB', 'EX15 3DR', 'EX15 3HG', 'EX15 3JJ', 'EX15 3JL', 'EX16 4NA', 'EX16 5AA', 'EX16 5AD', 'EX16 5AE', 'EX16 5HY', 'EX16 5JL', 'EX16 5JN', 'EX16 5JP', 'EX16 5JR', 'EX16 5JT', 'EX16 5JU', 'EX16 5JW', 'EX16 5LE', 'EX16 5LF', 'EX16 5LG', 'EX16 5LH', 'EX16 5LJ', 'EX16 5LQ', 'EX16 5LZ', 'EX16 5QG', 'EX16 6HA', 'EX16 6HX', 'EX16 6JQ', 'EX16 6RZ', 'EX16 6SB', 'EX16 6SW', 'EX16 6TG', 'EX16 6TR', 'EX16 7JH', 'EX16 7JQ', 'EX16 7RA', 'EX16 7RB', 'EX16 7RJ', 'EX16 8HD', 'EX16 8HJ', 'EX16 8LA', 'EX16 8PU', 'EX16 8RG', 'EX16 8RP', 'EX16 8RW', 'EX16 8SA', 'EX16 9AF', 'EX16 9AY', 'EX16 9DX', 'EX16 9JA', 'EX16 9PD', 'EX16 9PY', 'EX17 3DH', 'EX17 3PS', 'EX17 3QN', 'EX17 4SL', 'EX17 5AX', 'EX17 5HU', 'EX17 5JB', 'EX17 5LW', 'EX17 5PW', 'EX17 6HZ', 'EX18 7EA', 'EX18 7LF', 'EX18 7PL', 'EX18 7SL', 'EX19 8AZ', 'EX2 6LH', 'EX2 6LL', 'EX2 6LR', 'EX2 6LW', 'EX2 6LX', 'EX2 6LY', 'EX20 2AB', 'EX20 2AH', 'EX20 2AJ', 'EX20 2EE', 'EX20 2EF', 'EX20 2LZ', 'EX20 2NH', 'EX20 2NP', 'EX20 2NR', 'EX20 2SE', 'EX20 3EG', 'EX20 3NE', 'EX22 7UW', 'EX23 9BN', 'EX23 9BP', 'EX23 9BW', 'EX24 6QF', 'EX3 0PA', 'EX3 0PB', 'EX31 3JH', 'EX31 4AP', 'EX31 4AW', 'EX31 4HG', 'EX31 4LR', 'EX31 4QB', 'EX31 4RU', 'EX31 4ST', 'EX31 4TT', 'EX32 0LX', 'EX32 0LZ', 'EX32 0ND', 'EX32 0RJ', 'EX33 2NX', 'EX34 0AE', 'EX34 0AF', 'EX34 0AJ', 'EX34 0AN', 'EX34 0AQ', 'EX34 0NA', 'EX34 0PJ', 'EX35 6NX', 'EX36 3DT', 'EX36 3EP', 'EX36 3JH', 'EX36 3JJ', 'EX36 3JL', 'EX36 3LE', 'EX36 3LZ', 'EX36 3NW', 'EX36 3PS', 'EX36 4HX', 'EX36 4JJ', 'EX36 4JL', 'EX36 4LG', 'EX36 4LQ', 'EX36 4PN', 'EX36 4QD', 'EX36 4RT', 'EX37 9AB', 'EX37 9AG', 'EX37 9AR', 'EX37 9DA', 'EX37 9HR', 'EX37 9JS', 'EX37 9NB', 'EX37 9RE', 'EX38 7EJ', 'EX38 7HD', 'EX38 8AS', 'EX38 8AT', 'EX38 8AW', 'EX38 8JD', 'EX38 8JE', 'EX39 4QT', 'EX39 6DS', 'EX39 6DY', 'EX39 6EA', 'EX39 6HL', 'EX4 2HA', 'EX4 5AD', 'EX5 1BT', 'EX5 1BX', 'EX5 2NG', 'EX5 2NH', 'EX5 2NJ', 'EX5 4BR', 'EX5 4BS', 'EX5 4BT', 'EX5 4LD', 'EX5 5AB', 'EX5 5AE', 'EX5 5EQ', 'EX5 5LX', 'EX5 5LY', 'EX6 7HE', 'EX6 7PW', 'EX6 7QL', 'EX6 7QN', 'EX6 7TA', 'EX6 7TB', 'EX6 7TD', 'EX6 7TF', 'EX6 7TG', 'EX6 7TH', 'EX6 7TJ', 'EX6 7TL', 'EX6 7TN', 'EX6 7UT', 'EX6 8JS', 'EX7 9AE', 'EX7 9BJ', 'EX7 9PY', 'EX8 5ER', 'EX8 5EY', 'EX8 5EZ', 'EX8 5HH', 'EX8 5HQ', 'EX9 7AZ', 'PL10 1BY', 'PL12 5BG', 'PL13 2EP', 'PL13 2ER', 'PL13 2ES', 'PL13 2EX', 'PL14 3LJ', 'PL14 4QX', 'PL14 6NG', 'PL15 7NW', 'PL15 8DH', 'PL15 8EX', 'PL15 8UW', 'PL15 9QN', 'PL15 9QP', 'PL16 0AH', 'PL16 0AJ', 'PL16 0AL', 'PL16 0EL', 'PL17 8LQ', 'PL17 8NJ', 'PL17 8NL', 'PL19 9PR', 'PL20 6SG', 'PL20 7SL', 'PL20 7SP', 'PL20 7SS', 'PL20 7TG', 'PL20 7TJ', 'PL20 7TZ', 'PL21 0LL', 'PL21 9NT', 'PL24 2AD', 'PL24 2AE', 'PL24 2AF', 'PL24 2AG', 'PL24 2AH', 'PL24 2AJ', 'PL24 2AN', 'PL24 2AQ', 'PL24 2AR', 'PL24 2AT', 'PL24 2AW', 'PL24 2AX', 'PL24 2AY', 'PL24 2BB', 'PL24 2BD', 'PL24 2DH', 'PL24 2DN', 'PL24 2HY', 'PL24 2JA', 'PL24 2JB', 'PL24 2JD', 'PL24 2JF', 'PL24 2JG', 'PL24 2JH', 'PL24 2JQ', 'PL24 2LU', 'PL24 2LX', 'PL24 2LZ', 'PL24 2ND', 'PL24 2NF', 'PL24 2NJ', 'PL24 2NS', 'PL24 2NU', 'PL24 2NX', 'PL24 2NY', 'PL24 2PA', 'PL24 2PB', 'PL24 2PD', 'PL24 2PE', 'PL24 2RF', 'PL24 2RL', 'PL24 2RN', 'PL24 2TW', 'PL25 5BU', 'PL26 6BT', 'PL26 6BU', 'PL26 6BX', 'PL26 6BZ', 'PL26 6DA', 'PL26 6DB', 'PL26 6DD', 'PL26 6DG', 'PL26 6RZ', 'PL26 7AA', 'PL26 7AD', 'PL26 7AE', 'PL26 7AR', 'PL26 7AS', 'PL26 7AX', 'PL26 7AY', 'PL26 7LL', 'PL3 6EE', 'PL30 5LF', 'PL30 5LL', 'PL32 9PB', 'PL32 9PD', 'PL32 9PG', 'PL32 9TL', 'PL5 4AQ', 'PL5 4LD', 'PL5 4NB', 'PL5 4NG', 'PL5 4NH', 'PL5 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Q14 8SX', 'TQ14 8SY', 'TQ14 8SZ', 'TQ14 8TB', 'TQ7 2DN', 'TQ7 2QD', 'TQ7 2RE', 'TQ7 4DS', 'TQ9 6AH', 'TQ9 6NE', 'TQ9 6PD', 'TQ9 6RH', 'TQ9 6RL', 'TQ9 7AE', 'TQ9 7JT', 'TQ9 7LN', 'TQ9 7SR', 'TQ9 7SS', 'TQ9 7SU', 'TQ9 7SX', 'TQ9 7SZ', 'TQ9 7TA', 'TQ9 7TB', 'TQ9 7TH', 'TQ9 7TJ', 'TQ9 7TL', 'TQ9 7TP', 'TQ9 7TQ', 'TQ9 7TR', 'TQ9 7TS', 'TQ9 7TT', 'TQ9 7TU', 'TQ9 7TX', 'TQ9 7TY', 'TQ9 7UH', 'TR13 0PF', 'TR13 0RA', 'TR13 8HG', 'TR13 8HP', 'TR13 8HR', 'TR13 8HW', 'TR13 8HZ', 'TR17 0HJ', 'TR17 0HN', 'TR17 0HQ', 'TR18 3LL', 'TR19 7PG', 'TR19 7PH', 'TR19 7PN', 'TR19 7PQ', 'TR19 7PX', 'TR19 7TW', 'TR2 4AU', 'TR2 4EX', 'TR2 4HX', 'TR2 4PG', 'TR2 4PH', 'TR2 4PP', 'TR2 4PQ', 'TR2 4QB', 'TR2 4RU', 'TR2 4RX', 'TR2 4SB', 'TR20 9BL', 'TR3 6AA', 'TR3 7BT', 'TR3 7BU', 'TR3 7BX', 'TR3 7BY', 'TR3 7ND', 'TR4 8QZ', 'TR4 8RA', 'TR4 8RE', 'TR4 8RL', 'TR4 8RN', 'TR4 8SU', 'TR4 9PE', 'TR4 9QT', 'TR7 2HX', 'TR7 2RW', 'TR7 2TG', 'TR7 3BS', 'TR7 3PE', 'TR8 4BJ', 'TR8 4EP', 'TR8 4JW', 'TR8 4PU', 'TR8 4QE', 'TR8 5PS', 'TR9 6BD', 'TR9 6BY');

bokeh
02-19-2006, 10:03 AM
It works for me. This is the exact test script that I ran.<?php

$postal = 'Ex37 9AC';
echo '<pre>EX37 9AC ';var_dump(checkPostcode ($postal));echo '<pre>';
echo $postal;


function checkPostcode (&$postcode)
{
$banned = array('EX10 0AF', 'EX10 0JX', 'EX10 0LB', 'EX10 OLD', 'EX10 OLJ', 'EX10 OLW', 'EX10 OLD', 'EX10 ONG', 'EX10 OSF', 'EX10 9PN', 'EX10 OLD', 'EX11 1LU', 'EX11 1NF', 'EX12 4AF', 'EX13 5RS', 'EX13 7LF', 'EX13 7LN', 'EX13 7LW', 'EX13 7NN', 'EX13 7PE', 'EX13 7PG', 'EX13 7PJ', 'EX13 7PN', 'EX13 7PP', 'EX13 7PQ', 'EX13 7RG', 'EX14 3HE', 'EX14 3NZ', 'EX15 1BH', 'EX15 1BS', 'EX15 1BW', 'EX15 1NX', 'EX15 1PA', 'EX15 1QL', 'EX15 1XL', 'EX15 2LQ', 'EX15 2ND', 'EX15 2NW', 'EX15 2RB', 'EX15 3AR', 'EX15 3DB', 'EX15 3DR', 'EX15 3HG', 'EX15 3JJ', 'EX15 3JL', 'EX16 4NA', 'EX16 5AA', 'EX16 5AD', 'EX16 5AE', 'EX16 5HY', 'EX16 5JL', 'EX16 5JN', 'EX16 5JP', 'EX16 5JR', 'EX16 5JT', 'EX16 5JU', 'EX16 5JW', 'EX16 5LE', 'EX16 5LF', 'EX16 5LG', 'EX16 5LH', 'EX16 5LJ', 'EX16 5LQ', 'EX16 5LZ', 'EX16 5QG', 'EX16 6HA', 'EX16 6HX', 'EX16 6JQ', 'EX16 6RZ', 'EX16 6SB', 'EX16 6SW', 'EX16 6TG', 'EX16 6TR', 'EX16 7JH', 'EX16 7JQ', 'EX16 7RA', 'EX16 7RB', 'EX16 7RJ', 'EX16 8HD', 'EX16 8HJ', 'EX16 8LA', 'EX16 8PU', 'EX16 8RG', 'EX16 8RP', 'EX16 8RW', 'EX16 8SA', 'EX16 9AF', 'EX16 9AY', 'EX16 9DX', 'EX16 9JA', 'EX16 9PD', 'EX16 9PY', 'EX17 3DH', 'EX17 3PS', 'EX17 3QN', 'EX17 4SL', 'EX17 5AX', 'EX17 5HU', 'EX17 5JB', 'EX17 5LW', 'EX17 5PW', 'EX17 6HZ', 'EX18 7EA', 'EX18 7LF', 'EX18 7PL', 'EX18 7SL', 'EX19 8AZ', 'EX2 6LH', 'EX2 6LL', 'EX2 6LR', 'EX2 6LW', 'EX2 6LX', 'EX2 6LY', 'EX20 2AB', 'EX20 2AH', 'EX20 2AJ', 'EX20 2EE', 'EX20 2EF', 'EX20 2LZ', 'EX20 2NH', 'EX20 2NP', 'EX20 2NR', 'EX20 2SE', 'EX20 3EG', 'EX20 3NE', 'EX22 7UW', 'EX23 9BN', 'EX23 9BP', 'EX23 9BW', 'EX24 6QF', 'EX3 0PA', 'EX3 0PB', 'EX31 3JH', 'EX31 4AP', 'EX31 4AW', 'EX31 4HG', 'EX31 4LR', 'EX31 4QB', 'EX31 4RU', 'EX31 4ST', 'EX31 4TT', 'EX32 0LX', 'EX32 0LZ', 'EX32 0ND', 'EX32 0RJ', 'EX33 2NX', 'EX34 0AE', 'EX34 0AF', 'EX34 0AJ', 'EX34 0AN', 'EX34 0AQ', 'EX34 0NA', 'EX34 0PJ', 'EX35 6NX', 'EX36 3DT', 'EX36 3EP', 'EX36 3JH', 'EX36 3JJ', 'EX36 3JL', 'EX36 3LE', 'EX36 3LZ', 'EX36 3NW', 'EX36 3PS', 'EX36 4HX', 'EX36 4JJ', 'EX36 4JL', 'EX36 4LG', 'EX36 4LQ', 'EX36 4PN', 'EX36 4QD', 'EX36 4RT', 'EX37 9AB', 'EX37 9AG', 'EX37 9AR', 'EX37 9DA', 'EX37 9HR', 'EX37 9JS', 'EX37 9NB', 'EX37 9RE', 'EX38 7EJ', 'EX38 7HD', 'EX38 8AS', 'EX38 8AT', 'EX38 8AW', 'EX38 8JD', 'EX38 8JE', 'EX39 4QT', 'EX39 6DS', 'EX39 6DY', 'EX39 6EA', 'EX39 6HL', 'EX4 2HA', 'EX4 5AD', 'EX5 1BT', 'EX5 1BX', 'EX5 2NG', 'EX5 2NH', 'EX5 2NJ', 'EX5 4BR', 'EX5 4BS', 'EX5 4BT', 'EX5 4LD', 'EX5 5AB', 'EX5 5AE', 'EX5 5EQ', 'EX5 5LX', 'EX5 5LY', 'EX6 7HE', 'EX6 7PW', 'EX6 7QL', 'EX6 7QN', 'EX6 7TA', 'EX6 7TB', 'EX6 7TD', 'EX6 7TF', 'EX6 7TG', 'EX6 7TH', 'EX6 7TJ', 'EX6 7TL', 'EX6 7TN', 'EX6 7UT', 'EX6 8JS', 'EX7 9AE', 'EX7 9BJ', 'EX7 9PY', 'EX8 5ER', 'EX8 5EY', 'EX8 5EZ', 'EX8 5HH', 'EX8 5HQ', 'EX9 7AZ', 'PL10 1BY', 'PL12 5BG', 'PL13 2EP', 'PL13 2ER', 'PL13 2ES', 'PL13 2EX', 'PL14 3LJ', 'PL14 4QX', 'PL14 6NG', 'PL15 7NW', 'PL15 8DH', 'PL15 8EX', 'PL15 8UW', 'PL15 9QN', 'PL15 9QP', 'PL16 0AH', 'PL16 0AJ', 'PL16 0AL', 'PL16 0EL', 'PL17 8LQ', 'PL17 8NJ', 'PL17 8NL', 'PL19 9PR', 'PL20 6SG', 'PL20 7SL', 'PL20 7SP', 'PL20 7SS', 'PL20 7TG', 'PL20 7TJ', 'PL20 7TZ', 'PL21 0LL', 'PL21 9NT', 'PL24 2AD', 'PL24 2AE', 'PL24 2AF', 'PL24 2AG', 'PL24 2AH', 'PL24 2AJ', 'PL24 2AN', 'PL24 2AQ', 'PL24 2AR', 'PL24 2AT', 'PL24 2AW', 'PL24 2AX', 'PL24 2AY', 'PL24 2BB', 'PL24 2BD', 'PL24 2DH', 'PL24 2DN', 'PL24 2HY', 'PL24 2JA', 'PL24 2JB', 'PL24 2JD', 'PL24 2JF', 'PL24 2JG', 'PL24 2JH', 'PL24 2JQ', 'PL24 2LU', 'PL24 2LX', 'PL24 2LZ', 'PL24 2ND', 'PL24 2NF', 'PL24 2NJ', 'PL24 2NS', 'PL24 2NU', 'PL24 2NX', 'PL24 2NY', 'PL24 2PA', 'PL24 2PB', 'PL24 2PD', 'PL24 2PE', 'PL24 2RF', 'PL24 2RL', 'PL24 2RN', 'PL24 2TW', 'PL25 5BU', 'PL26 6BT', 'PL26 6BU', 'PL26 6BX', 'PL26 6BZ', 'PL26 6DA', 'PL26 6DB', 'PL26 6DD', 'PL26 6DG', 'PL26 6RZ', 'PL26 7AA', 'PL26 7AD', 'PL26 7AE', 'PL26 7AR', 'PL26 7AS', 'PL26 7AX', 'PL26 7AY', 'PL26 7LL', 'PL3 6EE', 'PL30 5LF', 'PL30 5LL', 'PL32 9PB', 'PL32 9PD', 'PL32 9PG', 'PL32 9TL', 'PL5 4AQ', 'PL5 4LD', 'PL5 4NB', 'PL5 4NG', 'PL5 4NH', 'PL5 4NZ', 'PL7 1YB', 'PL8 2DY', 'PL8 2DZ', 'PL8 2EY', 'PL8 2LS', 'PL8 2LX', 'PL8 2NA', 'TQ10 9EF', 'TQ10 9ET', 'TQ10 9NB', 'TQ11 0AH', 'TQ11 0BA', 'TQ11 0BS', 'TQ11 0BT', 'TQ11 0BU', 'TQ11 0BY', 'TQ11 0BZ', 'TQ11 0EA', 'TQ11 0HE', 'TQ11 0NN', 'TQ11 0PF', 'TQ11 0QA', 'TQ12 3PF', 'TQ12 5UP', 'TQ12 6NL', 'TQ13 0NJ', 'TQ13 7DY', 'TQ13 7EJ', 'TQ13 7JG', 'TQ13 7QH', 'TQ13 7QL', 'TQ13 7QP', 'TQ13 7QW', 'TQ13 7RF', 'TQ13 7RN', 'TQ13 7TF', 'TQ13 7TG', 'TQ13 8JZ', 'TQ13 8LA', 'TQ13 8QY', 'TQ13 8SD', 'TQ13 9SS', 'TQ13 9SW', 'TQ13 9TB', 'TQ13 9TS', 'TQ14 8AB', 'TQ14 8AD', 'TQ14 8AE', 'TQ14 8AF', 'TQ14 8AH', 'TQ14 8AJ', 'TQ14 8AL', 'TQ14 8AS', 'TQ14 8AT', 'TQ14 8AU', 'TQ14 8AW', 'TQ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 4PQ', 'TR2 4QB', 'TR2 4RU', 'TR2 4RX', 'TR2 4SB', 'TR20 9BL', 'TR3 6AA', 'TR3 7BT', 'TR3 7BU', 'TR3 7BX', 'TR3 7BY', 'TR3 7ND', 'TR4 8QZ', 'TR4 8RA', 'TR4 8RE', 'TR4 8RL', 'TR4 8RN', 'TR4 8SU', 'TR4 9PE', 'TR4 9QT', 'TR7 2HX', 'TR7 2RW', 'TR7 2TG', 'TR7 3BS', 'TR7 3PE', 'TR8 4BJ', 'TR8 4EP', 'TR8 4JW', 'TR8 4PU', 'TR8 4QE', 'TR8 5PS', 'TR9 6BD', 'TR9 6BY');
$allowed = '/^(?!'.implode('|', $banned).')((?:ex|pl|tq|tr)[0-9]{1,2})\s?([0-9]{1}[a-z]{2})$/i';
if(!preg_match($allowed, $postcode, $matches)) return false;
$postcode = strtoupper ($matches[1] . ' ' . $matches [2]);
return true;
}

?>

bokeh
02-19-2006, 10:09 AM
The $banned array must eiter be inside the function as above or feed to it as in the example below.<?php

$postal = 'Ex37 9AC';
$banned = array('EX10 0AF', 'EX10 0JX', 'EX10 0LB', 'EX10 OLD', 'EX10 OLJ', 'EX10 OLW', 'EX10 OLD', 'EX10 ONG', 'EX10 OSF', 'EX10 9PN', 'EX10 OLD', 'EX11 1LU', 'EX11 1NF', 'EX12 4AF', 'EX13 5RS', 'EX13 7LF', 'EX13 7LN', 'EX13 7LW', 'EX13 7NN', 'EX13 7PE', 'EX13 7PG', 'EX13 7PJ', 'EX13 7PN', 'EX13 7PP', 'EX13 7PQ', 'EX13 7RG', 'EX14 3HE', 'EX14 3NZ', 'EX15 1BH', 'EX15 1BS', 'EX15 1BW', 'EX15 1NX', 'EX15 1PA', 'EX15 1QL', 'EX15 1XL', 'EX15 2LQ', 'EX15 2ND', 'EX15 2NW', 'EX15 2RB', 'EX15 3AR', 'EX15 3DB', 'EX15 3DR', 'EX15 3HG', 'EX15 3JJ', 'EX15 3JL', 'EX16 4NA', 'EX16 5AA', 'EX16 5AD', 'EX16 5AE', 'EX16 5HY', 'EX16 5JL', 'EX16 5JN', 'EX16 5JP', 'EX16 5JR', 'EX16 5JT', 'EX16 5JU', 'EX16 5JW', 'EX16 5LE', 'EX16 5LF', 'EX16 5LG', 'EX16 5LH', 'EX16 5LJ', 'EX16 5LQ', 'EX16 5LZ', 'EX16 5QG', 'EX16 6HA', 'EX16 6HX', 'EX16 6JQ', 'EX16 6RZ', 'EX16 6SB', 'EX16 6SW', 'EX16 6TG', 'EX16 6TR', 'EX16 7JH', 'EX16 7JQ', 'EX16 7RA', 'EX16 7RB', 'EX16 7RJ', 'EX16 8HD', 'EX16 8HJ', 'EX16 8LA', 'EX16 8PU', 'EX16 8RG', 'EX16 8RP', 'EX16 8RW', 'EX16 8SA', 'EX16 9AF', 'EX16 9AY', 'EX16 9DX', 'EX16 9JA', 'EX16 9PD', 'EX16 9PY', 'EX17 3DH', 'EX17 3PS', 'EX17 3QN', 'EX17 4SL', 'EX17 5AX', 'EX17 5HU', 'EX17 5JB', 'EX17 5LW', 'EX17 5PW', 'EX17 6HZ', 'EX18 7EA', 'EX18 7LF', 'EX18 7PL', 'EX18 7SL', 'EX19 8AZ', 'EX2 6LH', 'EX2 6LL', 'EX2 6LR', 'EX2 6LW', 'EX2 6LX', 'EX2 6LY', 'EX20 2AB', 'EX20 2AH', 'EX20 2AJ', 'EX20 2EE', 'EX20 2EF', 'EX20 2LZ', 'EX20 2NH', 'EX20 2NP', 'EX20 2NR', 'EX20 2SE', 'EX20 3EG', 'EX20 3NE', 'EX22 7UW', 'EX23 9BN', 'EX23 9BP', 'EX23 9BW', 'EX24 6QF', 'EX3 0PA', 'EX3 0PB', 'EX31 3JH', 'EX31 4AP', 'EX31 4AW', 'EX31 4HG', 'EX31 4LR', 'EX31 4QB', 'EX31 4RU', 'EX31 4ST', 'EX31 4TT', 'EX32 0LX', 'EX32 0LZ', 'EX32 0ND', 'EX32 0RJ', 'EX33 2NX', 'EX34 0AE', 'EX34 0AF', 'EX34 0AJ', 'EX34 0AN', 'EX34 0AQ', 'EX34 0NA', 'EX34 0PJ', 'EX35 6NX', 'EX36 3DT', 'EX36 3EP', 'EX36 3JH', 'EX36 3JJ', 'EX36 3JL', 'EX36 3LE', 'EX36 3LZ', 'EX36 3NW', 'EX36 3PS', 'EX36 4HX', 'EX36 4JJ', 'EX36 4JL', 'EX36 4LG', 'EX36 4LQ', 'EX36 4PN', 'EX36 4QD', 'EX36 4RT', 'EX37 9AB', 'EX37 9AG', 'EX37 9AR', 'EX37 9DA', 'EX37 9HR', 'EX37 9JS', 'EX37 9NB', 'EX37 9RE', 'EX38 7EJ', 'EX38 7HD', 'EX38 8AS', 'EX38 8AT', 'EX38 8AW', 'EX38 8JD', 'EX38 8JE', 'EX39 4QT', 'EX39 6DS', 'EX39 6DY', 'EX39 6EA', 'EX39 6HL', 'EX4 2HA', 'EX4 5AD', 'EX5 1BT', 'EX5 1BX', 'EX5 2NG', 'EX5 2NH', 'EX5 2NJ', 'EX5 4BR', 'EX5 4BS', 'EX5 4BT', 'EX5 4LD', 'EX5 5AB', 'EX5 5AE', 'EX5 5EQ', 'EX5 5LX', 'EX5 5LY', 'EX6 7HE', 'EX6 7PW', 'EX6 7QL', 'EX6 7QN', 'EX6 7TA', 'EX6 7TB', 'EX6 7TD', 'EX6 7TF', 'EX6 7TG', 'EX6 7TH', 'EX6 7TJ', 'EX6 7TL', 'EX6 7TN', 'EX6 7UT', 'EX6 8JS', 'EX7 9AE', 'EX7 9BJ', 'EX7 9PY', 'EX8 5ER', 'EX8 5EY', 'EX8 5EZ', 'EX8 5HH', 'EX8 5HQ', 'EX9 7AZ', 'PL10 1BY', 'PL12 5BG', 'PL13 2EP', 'PL13 2ER', 'PL13 2ES', 'PL13 2EX', 'PL14 3LJ', 'PL14 4QX', 'PL14 6NG', 'PL15 7NW', 'PL15 8DH', 'PL15 8EX', 'PL15 8UW', 'PL15 9QN', 'PL15 9QP', 'PL16 0AH', 'PL16 0AJ', 'PL16 0AL', 'PL16 0EL', 'PL17 8LQ', 'PL17 8NJ', 'PL17 8NL', 'PL19 9PR', 'PL20 6SG', 'PL20 7SL', 'PL20 7SP', 'PL20 7SS', 'PL20 7TG', 'PL20 7TJ', 'PL20 7TZ', 'PL21 0LL', 'PL21 9NT', 'PL24 2AD', 'PL24 2AE', 'PL24 2AF', 'PL24 2AG', 'PL24 2AH', 'PL24 2AJ', 'PL24 2AN', 'PL24 2AQ', 'PL24 2AR', 'PL24 2AT', 'PL24 2AW', 'PL24 2AX', 'PL24 2AY', 'PL24 2BB', 'PL24 2BD', 'PL24 2DH', 'PL24 2DN', 'PL24 2HY', 'PL24 2JA', 'PL24 2JB', 'PL24 2JD', 'PL24 2JF', 'PL24 2JG', 'PL24 2JH', 'PL24 2JQ', 'PL24 2LU', 'PL24 2LX', 'PL24 2LZ', 'PL24 2ND', 'PL24 2NF', 'PL24 2NJ', 'PL24 2NS', 'PL24 2NU', 'PL24 2NX', 'PL24 2NY', 'PL24 2PA', 'PL24 2PB', 'PL24 2PD', 'PL24 2PE', 'PL24 2RF', 'PL24 2RL', 'PL24 2RN', 'PL24 2TW', 'PL25 5BU', 'PL26 6BT', 'PL26 6BU', 'PL26 6BX', 'PL26 6BZ', 'PL26 6DA', 'PL26 6DB', 'PL26 6DD', 'PL26 6DG', 'PL26 6RZ', 'PL26 7AA', 'PL26 7AD', 'PL26 7AE', 'PL26 7AR', 'PL26 7AS', 'PL26 7AX', 'PL26 7AY', 'PL26 7LL', 'PL3 6EE', 'PL30 5LF', 'PL30 5LL', 'PL32 9PB', 'PL32 9PD', 'PL32 9PG', 'PL32 9TL', 'PL5 4AQ', 'PL5 4LD', 'PL5 4NB', 'PL5 4NG', 'PL5 4NH', 'PL5 4NZ', 'PL7 1YB', 'PL8 2DY', 'PL8 2DZ', 'PL8 2EY', 'PL8 2LS', 'PL8 2LX', 'PL8 2NA', 'TQ10 9EF', 'TQ10 9ET', 'TQ10 9NB', 'TQ11 0AH', 'TQ11 0BA', 'TQ11 0BS', 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TQ9 7AE', 'TQ9 7JT', 'TQ9 7LN', 'TQ9 7SR', 'TQ9 7SS', 'TQ9 7SU', 'TQ9 7SX', 'TQ9 7SZ', 'TQ9 7TA', 'TQ9 7TB', 'TQ9 7TH', 'TQ9 7TJ', 'TQ9 7TL', 'TQ9 7TP', 'TQ9 7TQ', 'TQ9 7TR', 'TQ9 7TS', 'TQ9 7TT', 'TQ9 7TU', 'TQ9 7TX', 'TQ9 7TY', 'TQ9 7UH', 'TR13 0PF', 'TR13 0RA', 'TR13 8HG', 'TR13 8HP', 'TR13 8HR', 'TR13 8HW', 'TR13 8HZ', 'TR17 0HJ', 'TR17 0HN', 'TR17 0HQ', 'TR18 3LL', 'TR19 7PG', 'TR19 7PH', 'TR19 7PN', 'TR19 7PQ', 'TR19 7PX', 'TR19 7TW', 'TR2 4AU', 'TR2 4EX', 'TR2 4HX', 'TR2 4PG', 'TR2 4PH', 'TR2 4PP', 'TR2 4PQ', 'TR2 4QB', 'TR2 4RU', 'TR2 4RX', 'TR2 4SB', 'TR20 9BL', 'TR3 6AA', 'TR3 7BT', 'TR3 7BU', 'TR3 7BX', 'TR3 7BY', 'TR3 7ND', 'TR4 8QZ', 'TR4 8RA', 'TR4 8RE', 'TR4 8RL', 'TR4 8RN', 'TR4 8SU', 'TR4 9PE', 'TR4 9QT', 'TR7 2HX', 'TR7 2RW', 'TR7 2TG', 'TR7 3BS', 'TR7 3PE', 'TR8 4BJ', 'TR8 4EP', 'TR8 4JW', 'TR8 4PU', 'TR8 4QE', 'TR8 5PS', 'TR9 6BD', 'TR9 6BY');
echo '<pre>EX37 9AC ';var_dump(checkPostcode ($postal, $banned));echo '<pre>';
echo $postal;


function checkPostcode (&$postcode, $banned)
{
$allowed = '/^(?!'.implode('|', $banned).')((?:ex|pl|tq|tr)[0-9]{1,2})\s?([0-9]{1}[a-z]{2})$/i';
if(!preg_match($allowed, $postcode, $matches)) return false;
$postcode = strtoupper ($matches[1] . ' ' . $matches [2]);
return true;
}

?>There are other ways to do this also (like global) but since they are bad practice I would stick to one of the above.

peteyb
02-19-2006, 10:30 AM
cool. ive copied yours a across and its working fine.

thanks for all your help on this.

do you know of any good credi card validation scripts? (which hopefully validate Visa, Mastercard, Switch, Solo and Maestro.)

bokeh
02-19-2006, 10:41 AM
I would have thought that would be the job of your merchant account.

peteyb
02-19-2006, 10:54 AM
My boss would prefer to handle all transactions internally.

Is there a legal requirement to use a merchant trader when sending credit card details over the net?

bokeh
02-19-2006, 11:02 AM
What I am trying to say is that just like an email address there is no way to know if a card is good until you try to use it. Are you going to be checking the details real time with your bank?

peteyb
02-19-2006, 11:27 AM
We will be taking the details given from the MySQL databse at regular intervals and manually typing them into the PDQ machine (as we would if a client came in physically).

If they do not work we will contact the client and confirm details.

What is real-time?

NogDog
02-19-2006, 11:33 AM
1. I hope you'll be using a secure connection (https) so that users' card numbers cannot be easily intercepted by a sniffer?

2. Are you just looking for a regexp or such to confirm that the card number is the appropriate number of digits?

peteyb
02-19-2006, 11:38 AM
The site will have a secure socket layer in place provided by the Host company.

Im looking for something that checks the type of card against the number input.

NogDog
02-19-2006, 11:43 AM
You might want to look here: http://www.analysisandsolutions.com/software/ccvs/ (I can't vouch for its quality, just the first promising thing I found on Google).

peteyb
02-19-2006, 11:46 AM
thats the exact file ive been playing with for the last 2 hours but im struggling top get it to work.

oh well, i think thats all for me today. gonna take a step back from it all and look at it again 2mo.

if anyone cracks how to use the analysis and solutions ccvs file in the mean time post the finished code would ya!!!

peteyb
02-20-2006, 04:13 AM
Going back to the postcode section of the thread.

As there is no way of differentiating whether a user has mistyped their postcode or are in the banned list / outside of the four acceptable postcodes and alerting them of the difference i was thinking of inserting the list into MySQL database and allowing them to search to see if they are on the list.

Is this feasable and how could it be coded?

bokeh
02-20-2006, 05:16 AM
<?php

function checkPostcode (&$postcode, $banned, &$error)
{
$banned = '/^('.implode('|', $banned).')/i';
if(preg_match($banned, $postcode))
{
$error = 'Banned postcode!';
return false;
}
$prefixes = '/^(ex|pl|tq|tr)/i';
if(!preg_match($prefixes, $postcode))
{
$error = 'Unknown prefix!';
return false;
}
$syntax = '/^([a-z]{1,2}[0-9]{1,2})\s?([0-9]{1}[a-z]{2})$/i';
if(!preg_match($syntax, $postcode, $matches))
{
$error = 'Syntax error!';
return false;
}
$postcode = strtoupper ($matches[1] . ' ' . $matches [2]);
$error = 'No error!';
return true;
}

$banned = array('EX10 0AF', 'EX10 0JX', 'EX10 0LB', 'EX10 OLD', 'etc', 'etc', 'etc');

// Start: test it
$postal = 'eX9 1Jx';
echo 'Before: '.$postal.'<br>';
var_dump(checkPostcode ($postal, $banned, $error));
echo '<br> After: '.$postal.' <br>Error: '.$error.'<br><br>';

$postal = 'EX10 0JX';
echo 'Before: '.$postal.'<br>';
var_dump(checkPostcode ($postal, $banned, $error));
echo '<br> After: '.$postal.' <br>Error: '.$error.'<br><br>';

$postal = 'HA4 7ad';
echo 'Before: '.$postal.'<br>';
var_dump(checkPostcode ($postal, $banned, $error));
echo '<br> After: '.$postal.' <br>Error: '.$error.'<br><br>';

$postal = 'EX10 0J2';
echo 'Before: '.$postal.'<br>';
var_dump(checkPostcode ($postal, $banned, $error));
echo '<br> After: '.$postal.' <br>Error: '.$error.'<br><br>';
// End: test it

?>

peteyb
02-20-2006, 06:35 AM
Thats letting EX10 0LD through when typed but not when copied and pasted from the banned array code.

peteyb
02-20-2006, 06:39 AM
found the problem. The zeros had turned into the letter 'O'.

i think the codes as it should be now. good thread. i personally think it should go down in the history books!!!!!hehe