Help with OR in IF statement ( code included )

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

Help with OR in IF statement ( code included )

Post 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
paul00
Forum Newbie
Posts: 13
Joined: Sat Dec 20, 2008 2:54 pm

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

Post 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
}
 
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

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

Post 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.
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

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

Post 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';
paul00
Forum Newbie
Posts: 13
Joined: Sat Dec 20, 2008 2:54 pm

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

Post 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
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

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

Post 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
Post Reply