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})$';
// 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})$';
// 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})$';
// 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.
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.
?>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.