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
Help with OR in IF statement ( code included )
Moderator: General Moderators
-
pepe_lepew1962
- Forum Commoner
- Posts: 44
- Joined: Thu Nov 20, 2008 10:29 am
Re: Help with OR in IF statement ( code included )
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 )
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 )
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';
$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 )
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
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 )
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
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