We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5544
    • 8 Posts
    I am trying to validate the length of a zip code to 5 digits. Below is my PHP validation function:

    <?php
    function checkZipCode( &$fields )
           {
               
    $zipcode = $fields[ZipCode]; 
    $zipstring = (string)$zipcode;
    $length=strlen($zipstring); 
    
    if ($length == 5){
          return true;
          }else{
    return false;
    }
    }
    ?>


    and my input field looks like

    <input name="ZipCode" id="zipcode" class="text" type="text" eform="Your Zip Code:string:1:must be 5 numerals:#FUNCTION checkZipCode"/></label>


    Of course, I called the "checkZipCode" snippet on the page as well. The problem that I’m having is that any input I give it returns as false, even if I input 5 digits. Am I showing my PHP greenness here? Am I not using the function validation properly?

    Thank you for all of your help.
      • 10487 MODX Staff
      • 1,535 Posts
      This looks dodgy:
      $zipcode = $fields[ZipCode]; 

      Should it not be (note the quotes in the array reference):
      $zipcode = $fields['ZipCode']; 

      Also, forget casting the zipcode to a string, let PHP do that for you automagically.
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 3749
        • 24,544 Posts
        Also, unless you plan to modify the zip code in the function, you don’t need the reference operator.

        function checkZipCode( &$fields )

        might as well be
        function checkZipCode( $fields )


        In fact, you could probably do away with the function entirely and just use:

        if (strlen($fields[’zipcode’]) != 5) {
        // do something
        } else {
        // do something else
        }

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 5544
          • 8 Posts
          Thank you for the quick replies. I changed the function to look like
          <?php
          function checkZipCode( $fields )
                 {
                     
          if (strlen($fields['ZipCode']) != 5) {
            return false;
          } else  {
            return true;
          }
          }
          ?>


          yet I am still getting an error that my zipcode isn’t 5 characters, even when I put in "45632". I’m quite stumped.
            • 3749
            • 24,544 Posts
            Try putting
            echo "FIELDS: ";
            print_r($fields); 

            in the function. It will show you what’s arriving and what the actual array keys are.

            If it’s blank, you have to move back up through your code to see why.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 5544
              • 8 Posts
              Ah HA! Thanks BobRay. It turns out that the only value that $fields had was the zipcode number. That was it. There was no array as we had thought. So, I changed the validation code to
              <?php
              function checkZipCode( $fields )
                     {
                        
              if (strlen($fields) != 5) {
               return false;
              } else  {
                return true;
              }
              }
              ?>


              and it works. My thinking is that I called the function on ONLY the zipcode field, not as the eFormOnValidate event. Thus, $fields was passing only the value of the zipcode field, not the entire array. Am I correct in thinking this way?

              Again, thank you BobRay and garryn for all of your support. The MODx community is truly outstanding.
                • 3749
                • 24,544 Posts
                Quote from: cmuluna at Feb 22, 2009, 08:31 PM

                My thinking is that I called the function on ONLY the zipcode field, not as the eFormOnValidate event. Thus, $fields was passing only the value of the zipcode field, not the entire array. Am I correct in thinking this way?

                That sounds right. Glad you got it sorted. smiley
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting