PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Question on Postcode / Zipcode Filter in multi-page form

  1. mrhoneyfoot
    Member

    Hey there,

    Firstly, Gravity forms are awesome. Great work guys..! Secondly, can you help solve a problem..?

    As the first part of a multi-page form, I need to filter customers based on where they live within the United Kingdom and thereby if they are eligible for a service. The structure of the form needs to be as follows:

    Firstly, the first page need contain only one field for postcode / zipcode. Is this possible with gravity forms?

    Secondly, I need this field to check the users postcode or postcode prefix against a list of covered postcodes or postcode prefixes. Is this possible? How would I define the list? I'd guess manually. But how?

    Thirdly, if they live in a postcode that is eligible, they should continue with the rest of the form. If the user is in a postcode that is ineligible, they should be shown a field saying 'sorry we don't presently cover your area'. Conditional logic makes your forms awesome, but given the postcode element, is this achievable?

    Best,

    MrHoneyfoot

    Posted 13 years ago on Tuesday January 18, 2011 | Permalink
  2. As a Gravity Forms user, I am going to say this is all possible.

    1 for sure is possible.

    2, you are going to have to do custom validation. Alex Cancado posted a snippet a while back about doing this:
    http://forum.gravityhelp.com/topic/custom-field-that-supports-validation-by-regular-expressions#post-12652

    Once you validate against your array of postcodes, you can send them to the rest of the form (or maybe a new form) or display the "sorry, we don't support your area."

    Hopefully that will get the wheels turning until David Smith comes along and can post more detail on your customization.

    I see this as two forms: 1 form that accepts the postcode and does the validation, then based on whether or not the area is covered, either display the "sorry we don't cover your area" message as a validation error, or send the postcode to form #2 and continue with the form.

    A neat application for sure that I am pretty confident is possible with Gravity Forms.

    Posted 13 years ago on Tuesday January 18, 2011 | Permalink
  3. mrhoneyfoot
    Member

    Well that's reassuring. I don't fully understand Alex Cancado's snippet, or where it goes. Need some guidance on that. The two form solution sounds very neat.

    Thank you illinoisharley. I like your website.

    Posted 13 years ago on Tuesday January 18, 2011 | Permalink
  4. The snippet would go in your theme's functions.php. I may try this and see how it all works.

    Posted 13 years ago on Tuesday January 18, 2011 | Permalink
  5. Secondly, I need this field to check the users postcode or postcode prefix against a list of covered postcodes or postcode prefixes. Is this possible? How would I define the list? I'd guess manually. But how?

    How many postcodes will you be checking against? Let me know and I'll get you some sample code a little closer to your desired end goal.

    @illinoisharley, thanks for all the assistance on the forums! You rock!

    Posted 13 years ago on Tuesday January 18, 2011 | Permalink
  6. mrhoneyfoot
    Member

    Hi David,

    I want to cover around 280 postcode districts out of a full list of 2,971 in the UK. So entries need to be checked against the full list to catch typos, before checking against the smaller list to determine eligibility.

    There would need to be a third outcome for the user to the first part of the form.

    Firstly, yes you are covered - continue to the quote form.
    Secondly, sorry you're not covered.
    Thirdly, sorry we didn't recognise your postcode, please try again.

    So I might be wrong, I frequently am, but are two seperate validations needed?

    1. Is it on the list of valid postcode districts? If YES, check against smaller list, if NO, display message "sorry we didn't recognise your postcode, please try again."

    2. If it is valid, is it on the smaller list of covered postcodes? If YES, continue to quote form (as illinoisharley suggests - a separate form), if No, display message "sorry you're not covered".

    @ illinoisharley, thanks for your help and interest, the solution to this is gona be intriguing.

    Posted 13 years ago on Wednesday January 19, 2011 | Permalink
  7. mrhoneyfoot
    Member

    Now then, the lists have been made up. David Smith, I hope I'm not flying too close to the sun with this one. If you or anyone else thinks it's madness, let me know. Or has anybody heard of another solution?

    Posted 13 years ago on Saturday January 22, 2011 | Permalink
  8. Try this:

    http://pastie.org/1487205

    It looks like a lot more than it is because it is heavily commented.

    Posted 13 years ago on Saturday January 22, 2011 | Permalink
  9. mrhoneyfoot
    Member

    Thanks for the snippet David Smith. I'm having two problems with it. Firstly, changing the field IDs as you directed makes the validation apply to every instance of that field ID across all forms. So is there a way for this validation to apply to only one unique form?

    Secondly, is there a way for your code to validate only the prefix (first 2-4 characters)? People are going to input their full postcode but a full postcode list would be impractical (240,000 postcodes). Using the 240 postcode prefixes in the array means that any full postcode is invalidated, even if the prefix is in the array. So does anyone know of a way to make the validation apply only to the first 2-4 characters of the postcode, ignoring the rest?

    Cheers guys.

    Posted 13 years ago on Monday January 24, 2011 | Permalink
  10. mrhoneyfoot
    Member

    No worries on the first problem, David Smith. Changed if($field['id'] != 2) to if($field['formId'] != 5 || $field['id'] != 1)So now it only applies to that one form.

    Still stuck with the second. Any ideas mate? Found some UK postcode lists in .csv and .sql if that makes a difference.

    Posted 13 years ago on Monday January 24, 2011 | Permalink
  11. Assuming that the post code prefix is broken up by some sort of designator (ie US post codes are 23462-1234), you could use the php explode() function to split the submitted zipcode and just test against the prefix.

    http://php.net/manual/en/function.explode.php

    Posted 13 years ago on Monday January 24, 2011 | Permalink
  12. mrhoneyfoot
    Member

    Thanks for the pointer. Am now validating only the prefix against the array, even if a full postcode is entered.

    Quick question though. I have an sql db setup that has a table of all UK prefixes. After the main if(in_array()) check in your code above, I want to insert a further check against the sql db. To catch postcodes not in the first array that are nonetheless correct, and return some other result or message for the user. Before the point where an entry triggers the failed validation message, therefore.

    So my question is this. Is it possible to insert another if(in__another_array()) check to return a separate validation outcome?

    Cheers guys. Hope I'm not being too much of a handful.

    Posted 13 years ago on Tuesday January 25, 2011 | Permalink
  13. mrhoneyfoot
    Member

    Right then second array populated via mysql. My goal is to make that second validation result appear as a specific gforms validation_message.

    Back to David Smith's last snippet. What I want to do is display a custom validation message if the postcode is in the second array. What we have so far is this:

    http://www.pastie.org/1499817

    So my question is how do I link the second in_array check to the validation message directly under it?

    The form uses only the last validation message.

    Posted 13 years ago on Wednesday January 26, 2011 | Permalink
  14. If you are only getting the last error message ("Your postcode is not recognised, please check and try again.") this is indicative that your if condition is not being met. Can you verify if $postcode evaluates to true AND that $valcode is actually in the $valcodes?

    Posted 13 years ago on Wednesday January 26, 2011 | Permalink
  15. mrhoneyfoot
    Member

    Fine on the first count. Not 100% sure the second array is being correctly populated. I have it like this:

    http://www.pastie.org/1503163

    Am I doing it wrong?

    Posted 13 years ago on Thursday January 27, 2011 | Permalink
  16. Now that you're getting into more advanced territory there are two things you'll want to familiarize yourself with.

    1) The WordPress database object: http://codex.wordpress.org/Function_Reference/wpdb_Class

    2) PHP print_r() function: http://php.net/manual/en/function.print-r.php

    After querying the database using the $wpdb class, you can use print_r() to output the returned results to the screen so you can see specifically what you are working with. In this case, you need to print_r the $valcodes array to verify that it is indeed a list of the post codes you want.

    Posted 13 years ago on Thursday January 27, 2011 | Permalink
  17. Tom Bevington
    Member

    Just purchased a developers license of GF, and have a similar need to Mr. Honeyfoot.

    Namely I have a simple, five-field form, the last field being a zipcode that will be used to drive the user to either a) one of two different price-list pages.... or b) to a single page that conditionally displays one of the two aforementioned price-lists.

    Have the following questions:

    1) Can a single GF form with both visible and invisible fields span MORE THAN ONE WordPress Page?

    2) I have the JavaScript and Regular Expression code to do the Zipcode Test and don't want to use PHP, so how/where do I use my JS/RegEx code for the zipcode test in Gravity Forms?

    3) Any General ideas on how to best accomplish this in Gravity Forms given 1 and 2 above?

    Posted 13 years ago on Friday March 25, 2011 | Permalink
  18. 1) Gravity Forms 1.5 supports multi-page forms using the Page Break field.

    2) You would have to place any custom jQuery on the page containing the form.

    3) I'm not 100% sure, I would use jQuery and target the zipcode field and apply custom jQuery to manipulate the input however you want.

    Posted 13 years ago on Friday March 25, 2011 | Permalink
  19. Tom Bevington
    Member

    Carl, thanks for the reply, but need as a GF Newbie seek some clarification....

    RE: 1) Does GF 1.5's multi-page support via the Page Break Field allow me to have a single GF Form that spans multiple WORDPRESS PAGES? That is, if I have a GF Form with 15 fields, can I reveal and populate fields 1-5 with WORDPRESS PAGE A, fields 6-10 with WORDPRESS PAGE B, and fields 11-15 with WORDPRESS PAGE C? In essence, this means WORDPRESS PAGES would provide "Views" into selected portions of a GF form.

    RE: 2) My JavaScript Code to test the Zipcode is pretty simple, but I'm not sure how I'd leverage the jquery library that CF is already using to do the regex stuff. Can you point me to any examples or code showing how to use JS to test the value of a GF field, then based upon results of that calculation, setting a variable or making a calculation that can be used or accessed from an entirely different pages or forms? Here's the JS pcode for what I'm thinking:

    var zip5 = event.value.toString();

    var myRegExp = /^92(2|3|5)/;

    if ( zip5.length === 5 && myRegExp.test(zip5))
    {
    GOTO PAGE A WITH PRICE LIST A
    or.. GOTO PAGE C AND CALCULATE/DISPLAY PRICE LIST A
    }
    else
    {
    GOTO PAGE B WITH PRICE LIST B
    or.. GOTO PAGE C AND CALCULATE/DISPLACY PRICE LIST B
    }

    Posted 13 years ago on Friday March 25, 2011 | Permalink
  20. 1) No, a form is embedded on a single Page or Post. It doesn't span multiple Pages or Posts. If you wanted to do that you'd have to create multiple forms and embed them on each Page and then redirect to the next form as part of the confirmation process that happens when a form is submitted. But then the data would also span multiple forms and have to be accessed individually, it wouldn't be grouped.

    2) I'm not aware of any current examples on doing field validation with Javascript. If you use jQuery it should be possible. We do validation on the server side and most users use the gform_validation hook to do custom validation that way.

    Posted 13 years ago on Friday March 25, 2011 | Permalink
  21. Tom Bevington
    Member

    Carl,

    I'm back and thought I'd clarify something, when collecting data I realize I don't need to use multiple WP pages, but merely conditionally display the appropriate sections of a single form, i.e.

    var zip5 = event.value.toString();

    var myRegExp = /^92(2|3|5)/;

    if ( zip5.length === 5 && myRegExp.test(zip5))
    {
    DISPLAY FORM SECTION A WITH PRICE LIST A

    }
    else
    {
    DISPLAY FORM SECTION B WITH PRICE LIST B

    }

    I'd still like to do the conditional testing of the zipcode against a list of several hundred 5-digit US zipcodes on the client, but have little experience working with JQuery and have seen no examples in this forum of how to use JQuery on the client-side to check GF fields or act upon them. If you have any such examples or could direct me to some, they would be a great help.

    Lastly, if I can't do client-side field checking and conditional branching, I guess I'll have to do it server side. This will obviously be slower, but maybe more straightforward - even for a PHP novice. Do you have examples of how I'd check a zipcode captured in, say, section 1 of a gravity form - and then based upon that zipcode being in found in one of three lists, conditionally display the next section of the gravity form with the associated fields/prices/etc.?

    Any help would be most welcome.

    Posted 12 years ago on Monday May 2, 2011 | Permalink
  22. What you want to do is very unique which is why no examples exist, it hasn't been done before to my knowledge.

    Conditional logic works being triggered by selection fields (drop down, radio button and checkboxes) and can't be triggered by a standard input field. I'm not aware of any examples of people implementing this type of conditional with Gravity Forms so I don't really have any code snippets or examples to be able to point you to to assist.

    What you want to do is complex, you may want to consider hiring a WordPress developer with Gravity Forms customization experience to handle this for you, otherwise you are going to have a difficult time implementing something like this.

    Posted 12 years ago on Monday May 2, 2011 | Permalink
  23. Tom Bevington
    Member

    Assuming GF fields have (or can be assigned) an ID, shouldn't Client-side javascript be to access those fields via the document.getElementById() function?

    As it appears I've hit the wall with client-side GF customization, do you have any suggestions or examples you could point me to on how to capture the zipcode on the client form and do the validation and conditional activities on the server? Perhaps with the gform_validation hook you mentioned earlier. I need to test the zipcode captured in GF against the zipcode field of approximately 1200 records that will have to be loaded into MySQL manually or via some plugin.
    If there's a match on a record, I'll need to extract the record's other fields, send them back down to the client to be prepopulate and display other GF fields on the client.

    Am running out of options and hope you can point me to a viable plan-B or plon-C since my ciient-side Plan-A has pushed the envelope of GF.

    Posted 12 years ago on Saturday May 7, 2011 | Permalink