Log in

View Full Version : Help with OR in IF statement ( code included )



pepe_lepew1962
12-21-2008, 05:36 PM
Hello:

I found some individual code that works perfectly for what I need. The following code checks the data on zip/postal codes ( US and Canadian ) for compliance. My problem is that I think I need something like an OR statement in my code. If preg does not match either us format or canadian format then $errZip. Can anyone help me on this please.


// Zip must be 5 digits
if(preg_match("/^\d{5}$/", $_POST["zip"]) === 0)
$errZip = '<class="errText">Zip must be 5 digits';
// Postal must be 6 alphsnumeric with either space or - seperator
if(preg_match("/^([A-CEGHJ-NPR-TV-Z]){1}[0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[ -][0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[0-9]{1}$/i",$_POST["zip"]) === 0)
$errZip = '<class="errText">Postal Code must be character and numeric';

Thanks

Twey
12-21-2008, 05:54 PM
What exactly is the problem with the code as it is? Other, of course, than that there is no <class> tag in HTML, and that it hasn't been closed. The server-side code looks workable, although I know zip about the ZIP.

Nile
12-21-2008, 06:02 PM
Try something like this:


$isTrue = true; //This is set to true if the postal code CAN be found

$string = "6 characters long.";

if(!$isTrue){
$errZip = "Zip code must be %s";
$errZip = sprintf($errZip, $string);
}
die($errZip);

I really don't know if that's what you want though. You've gotta explain more.

pepe_lepew1962
12-21-2008, 06:12 PM
Twey, the problem is that if the US postal code is correct then due to the criteria the canadian is wrong. Same if the canadian is correct, the code then says the US is wrong. Each on there own works. I am trying to get a dual check on the zipcode.

Nile
12-21-2008, 06:21 PM
This isn't tested but see if it works:

<?php
$seq[0] = preg_match("/^\d{5}$/", $_POST["zip"]);
$seq[1] = preg_match("/^([A-CEGHJ-NPR-TV-Z]){1}[0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[ -][0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[0-9]{1}$/i",$_POST["zip"]);
$set[0] = "<class=\"errText\">, Zip, 5 digits";
$set[1] = "<class=\"errText\">, Postal Code, character and numeric";
$errZip = '%s %s must be %s';
if(($seq[0] === 0) || ($seq[1] === 0))
{
if($seq[0] === 0)
{
$errZip[0] = sprintf($errZip,$set[0]);
}
if($seq[1] === 0)
{
$errZip[1] = sprintf($errZip,$set[1]);
}
$errZip = join('<br />',$errZip);
}
?>

Twey
12-21-2008, 10:21 PM
Then as you say, an or, or a negated and:
function validAmericanZip($s) {
return !!preg_match('/^\d{5}$/', $s);
}

function validCanadianZip($s) {
return !!preg_match('/^([A-CEGHJ-NPR-TV-Z]){1}[0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[ -][0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[0-9]{1}$/i', $s);
}

if (!validAmericanZip($_POST['zip']) && !validCanadianZip($_POST['zip']))
errorOut('Invalid ZIP!');

Nile
12-21-2008, 11:31 PM
I don't understand your code Twey, why would you use 3 !, thus reversing it 2 and making it back to 'if false'. Can you please explain?
Also, he wants OR, ||, not AND, &&, unless that's part of the !!!?

pepe_lepew1962
12-22-2008, 02:57 AM
Okay, here is what works in case anyone wants a text box for US and/or Candian postal codes. Thank all who helped.


if(preg_match("/^\d{5}$/", $_POST["zip"]) === 0 && preg_match("/^([A-CEGHJ-NPR-TV-Z]){1}[0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[ -][0-9]{1}[A-CEGHJ-NPR-TV-Z]{1}[0-9]{1}$/i",$_POST["zip"]) === 0)

Twey
12-22-2008, 06:07 AM
I used the functions for a reason: they greatly enhance legibility. Follow suit. Functions are good, and self-documenting if used properly.
I don't understand your code Twey, why would you use 3 !, thus reversing it 2 and making it back to 'if false'.The !! is a quick shortcut to type-convert the result to boolean. ! is a boolean negation, and has the handy side effect of converting a value to boolean if it isn't already. If we negate the negation with a second !, we can get that handy side effect alone and have an instant boolean conversion in just two characters.
Also, he wants OR, ||, not AND, &&, unless that's part of the !!!? !a && !b is equivalent to !(a || b). Personally I prefer the former; I think 'not a valid American ZIP and not a valid Canadian ZIP' reads more naturally in English than 'not (a valid American ZIP or a valid Canadian ZIP)'.

Nile
12-22-2008, 06:33 AM
Why didn't you just put the preg_match in ( and ) instead?

Twey
12-22-2008, 06:42 AM
For the purposes of readability and reusability. PHP may try to make these coding staples as awkward as possible, but at least we've still got the good old function available to us. :)

hmsnacker123
12-26-2008, 12:39 AM
Heres a tested function to check all US zipcodes:



<?php
function validate_zip($zipcode){
return preg_match('/^[0-9]{5}([- ]?[0-9]{4})?$/', $zipcode);
}
?>