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
Started 8 years ago by NickJ | 11 posts |
-
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 8 years ago # -
Forgot to add, I'm working with version 4.6.
Posted 8 years ago # -
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 8 years ago # -
Ping.
Any help would be greatly appreciated.Posted 8 years ago # -
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.comPosted 8 years ago # -
You could validate zip code with jquery / javascript through regular expression.
Posted 8 years ago # -
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 8 years ago # -
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 8 years ago # -
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 # -
ping
Posted 8 years ago # -
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 #
Reply
You must log in to post.