This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 4

Validating U.S. Zip Code for Zip +4


  1. NickJ
    Member

    I've found some documentation on this in the Forum, but the posts are 6 years old and it appears the code has changed significantly. I'd like to validate the U.S. Zip Code to include 5 digits plus the optional +4. Could you point me in the right direction? Any help is appreciated!

    Posted 9 years ago #
  2. NickJ
    Member

    Forgot to add, I'm working with version 4.6.

    Posted 9 years ago #
  3. christiansweatt
    Member

    I'd also be very interested in this, if you find the answer!
    Perhaps something that could be easily implemented as an optional feature (checkbox item) for the address field in future versions?

    Posted 9 years ago #
  4. NickJ
    Member

    Ping.
    Any help would be greatly appreciated.

    Posted 9 years ago #
  5. Saghalie
    Pro Member

    Perhaps send an email to support? Especially since this hasn't been answered in a while. If and when you do get a response, please post it here!

    Regards,

    Saghalie
    http://www.kcmhosting.com

    Posted 9 years ago #
  6. celsiusmarketing
    Member

    You could validate zip code with jquery / javascript through regular expression.

    Posted 9 years ago #
  7. yuniar

    Let say you have a form with id number 12629 and the text field you're using for the zip code is having id number 1 (check the html source of your form to see it).

    You can edit your "machform/includes/post-functions.php" file. Search around line 452 for this code:

    $error_elements = array();
    $table_data = array();

    below that block of code, add this code:

    if($form_id == 12629){
        $zip_code_element_id = 1;
    
        if (!preg_match("/"."^\d{5}([\-]?\d{4})?$"."/i",$user_input['element_'.$zip_code_element_id])){
    	$error_elements[$zip_code_element_id] = 'Incorrect zip code';
    	}
    }

    That should do it.
    Just make sure to adjust the above form id number and the zip code field number with your own.


    MachForm Founder

    Posted 9 years ago #
  8. NickJ
    Member

    Thanks. Having some issues with implementing this. The source code is showing the Zip Code to be element_6_5. I've tried the following iterations with no luck:

    $zip_code_element_id = 6;
    $zip_code_element_id = "6_5";
    $zip_code_element_id = 6_5;
    $zip_code_element_id = 5;

    Thanks.

    Posted 9 years ago #
  9. fla56
    Member

    Hi Yuniar, hi guys, thanks for the code, I've been able to modify it to help me build domain-specific email validation but I need a little more help with this:

    if($form_id == 66389){ $email_element_id = 9; if (!preg_match("/"."^([a-z0-9]+(?!.*(?:\+{2,}|\-{2,}|\.{2,}))(?:[\.+\-]{0,1}[a-z0-9])*@nhs.net|[a-z0-9]+(?!.*(?:\+{2,}|\-{2,}|\.{2,}))(?:[\.+\-]{0,1}[a-z0-9])*@.*\.nhs\.uk)$"."/",$user_input['element_'.$email_element_id])){ $error_elements[$email_element_id] = 'Non-NHS email. Please enter a valid address.'; } }

    I really need to be able to do this for any/all forms and any/all email fields, ideally on a per-field basis (and for machform logins), any ideas?

    Thanks in advance.

    David

    Posted 8 years ago #
  10. NickJ
    Member

    ping

    Posted 8 years ago #
  11. NickJ
    Member

    For anyone else interested, the code above works if you build the address fields individually on the form and not use the built in address field. That allows the variable $zip_code_element_id to be compared to a digit without the underscore. Thanks for the assist, yuniar.

    Posted 8 years ago #

RSS feed for this topic

Reply