Page 1 of 2 12 LastLast
Results 1 to 10 of 12

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

  1. #1
    Join Date
    Nov 2008
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with OR in IF statement ( code included )

    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try something like this:
    Code:
    $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.
    Jeremy | jfein.net

  4. #4
    Join Date
    Nov 2008
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  5. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This isn't tested but see if it works:
    PHP Code:
    <?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);
    }
    ?>
    Last edited by Nile; 12-21-2008 at 07:18 PM.
    Jeremy | jfein.net

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Then as you say, an or, or a negated and:
    Code:
    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!');
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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 !!!?
    Jeremy | jfein.net

  8. #8
    Join Date
    Nov 2008
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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)

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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)'.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Why didn't you just put the preg_match in ( and ) instead?
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •