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.

Custom Input Mask for a valid URL

  1. draney
    Member

    I am using a Custom Field (Post Field) to collect a web address and want to validate that the entry is a complete valid URL, ie. it must contain http://

    Is there a way to do this with an input mask?

    Posted 11 years ago on Friday February 15, 2013 | Permalink
  2. I don't know of an input mask to verify that what is entered is a fully qualified domain name. The regular expression to validate a URL is pretty complex in itself (here is one example http://stackoverflow.com/a/11811798 ), and the input mask can't be used with a regular expression. Validating the data that was input is different than forcing a specific format.

    You can use the gform_pre_submission_filter to check the data that was input, and add http:// to the beginning if the string does not being with http:// already.

    http://www.gravityhelp.com/documentation/page/Gform_pre_submission_filter

    You could also use a regular website (URL) field (in the form builder) then copy that from the URL field to the post custom field, again using the gform_pre_submission_filter. That way, you would have the benefit of the URL validation that Gravity Forms applies to website inputs.

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  3. draney
    Member

    Both of your ideas sound good, but I think I prefer the gform_pre_submission_filter because I am using a submission preview and all field entries are displayed, so the other option would show the web address twice.

    Can you give me any help writing the necessary gform_pre_submission_filter? I would want it to do as you suggested, add the http:// to the beginning IF it was not there already.

    Thanks!

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  4. The post custom field could be marked admin only so it would not appear in your preview. Do you still want to use the method of checking for http and if it's not there, add it? If you are using a submission preview, the value is likely to be incorrect when displayed, either way we do it. Is that OK (that the value display just as it was entered)?

    Posted 11 years ago on Monday February 18, 2013 | Permalink
  5. draney
    Member

    Yes, it is okay if the preview shows the incorrect version of the URL. Could you help me write the appropriate gform_pre_submission_filter to check for the presence of http:// and add it if it is missing?

    Posted 11 years ago on Friday March 1, 2013 | Permalink
  6. Take a look at this code: http://pastebin.com/LyKCMYbB

    That code will go in your theme's functions.php. You will need to change the ID of the form on line 4 and the ID of the custom field input where you accept the URL on lines 10 and 20.

    Posted 11 years ago on Monday March 4, 2013 | Permalink
  7. draney
    Member

    Thanks. One more question:

    I have 11 forms and I think they all have a Custom Field (Post Field) for a website address. I think I could make it apply to all the forms by leaving of the form ID (ie. changing gform_pre_submission_filter_338 to gform_pre_submission_filter) but the form field doesn't have the same ID on each form.

    Is the only solution to repeat the gform_pre_submission_filter code 11 times with the necessary form and field ID adjustments for each form? And is there any website performance hit to do this?

    Posted 11 years ago on Tuesday March 5, 2013 | Permalink
  8. There is no (or at least a very minimal) performance hit to repeat the function 11 times. However, you will need to rename each function so that the function name is not declared multiple times (which will result in a fatal error.) See below though for a different way.

    You could leave off the form ID and have one filter line, then in your function, tie the form ID to a field ID, and then run the code based on that. Basically, on line 6, you would check to see if the current form ID is one of the forms you need to run this function for. Then, if the form ID matches, assign the field ID to a variable.

    Take a look at this code: http://pastebin.com/iFZKhM6B

    You will need to change the $formfields array to hold all your form ID and the field ID for the URL field you want to check in each form. I only listed 4 forms, but you have 11 to do.

    [php]
    // follow this format
    'formID' => 'fieldID'

    Replace the old function and filter call with this one and let us know how it goes.

    Posted 11 years ago on Friday March 8, 2013 | Permalink
  9. draney
    Member

    Works like a charm. Thanks!!

    Posted 11 years ago on Friday March 8, 2013 | Permalink
  10. David Peralty

    Glad to hear you got this sorted out. I'm going to close this issue.

    Posted 11 years ago on Friday March 8, 2013 | Permalink

This topic has been resolved and has been closed to new replies.