Page 1 of 1

Help with OR in IF statement ( code included )

Posted: Sun Dec 21, 2008 10:25 am
by pepe_lepew1962
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

Re: Help with OR in IF statement ( code included )

Posted: Sun Dec 21, 2008 3:23 pm
by paul00
Do you mean like this??

Code: Select all

 
 
if(preg_match("/^\d{5}$/", $_POST["zip"]) == 0) {
     $errZip = '<class="errText">Zip must be 5 digits';
}
 
elseif(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';
} else {
//Your code
}
 

Re: Help with OR in IF statement ( code included )

Posted: Sun Dec 21, 2008 6:24 pm
by pepe_lepew1962
Paul, the problem is that if the first criteria is met, then the second one automatically fails ( which is not a true statement ). I am trying to figure out the OR aspect and combining the 2 pieces of code. Not having any luck, but worth a try...I think.

Re: Help with OR in IF statement ( code included )

Posted: Sun Dec 21, 2008 7:16 pm
by pepe_lepew1962
Paul, I kind of have it working now. My thinking was wrong. It has to be the AND because if both fail, then display an error message. If one of them is good, then the other is obsolete. Now, just to make it visually easier to ffind, review and edit I am trying to get the entire preg in one segement without the seq parts.

$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"]);
if(($seq[0] === 0) && ($seq[1] === 0))
$errZip = '<class="errText">Invalid Zip/Postal code';

Re: Help with OR in IF statement ( code included )

Posted: Sun Dec 21, 2008 8:04 pm
by paul00
Yeah i thought you were meaning it that way after. that would of been my answer that your got working.

So its working now ok?

Paul

Re: Help with OR in IF statement ( code included )

Posted: Sun Dec 21, 2008 8:59 pm
by pepe_lepew1962
Actually, got the following to work too:

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)

Thanks