• Resolved agreda

    (@agreda)


    Thanks for the great work!

    Is there any way to validate form field entries using this plugin? I don’t see a way to do it withing the Mailchimp form builder, without adding scripts to the embedded form code. I also didn’t see such a feature in the Yikes Form Customizer plugin.

    Specifically, we’d like to validate the Website field to limit submission only if it includes a specific domain.tld. (Another example might be to limit the email address to a specific domain.)

    Thanks in advance for any feedback. If this is not possible, please consider it a feature request!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Freddie

    (@fmixell)

    Hey @agreda,

    You can add a pattern to your field like so, just change the domain.com example to match the domain you wish to validate.

    
    add_filter('yikes-mailchimp-field-data', 'add_pattern_to_mailchimp_emails', 10, 3);
    
    function add_pattern_to_mailchimp_emails( $field_array, $field, $form_id  ) {
        if( isset( $field['merge'] ) && 'email' == $field['type'] ) {
            $field_array['pattern'] = 'pattern="[\w.%+-]+@domain\.com"';
        }
        return $field_array;
    }
    

    This could can be added to your functions.php file or by using a custom plugin. If you don’t know how to add this to your theme you’re probably better off adding it to a plugin.

    https://ww.wp.xz.cn/plugins/my-custom-functions/

    Copy and paste the snippet from above into the custom PHP section of the plugin.

    Let me know if you have any further questions or if this resolves your issue!

    Cheers,

    Freddie

    PS: Here’s an example of changing the pattern for a website. Let’s say your website was “yikesplugins.com” you would change it to [\w.%+-]+@yikesplugins\.com

    • This reply was modified 6 years, 9 months ago by Freddie.
    Thread Starter agreda

    (@agreda)

    Fantastic, I’ll give this a shot. Thank you @fmixell !

    ’email’ == $field[‘type’]

    It looks like this targets the ’email’ field. Am I mistaken, or would I just change that to ‘website’ to validate the domain submitted in the Website field?

    Also, am I correct in assuming this would affect all forms including that field?

    Thanks again.

    Plugin Contributor Freddie

    (@fmixell)

    @agreda,

    The example that I provided would be to limit the email submissions to a specific TLD but if you’d like to do it for the URL fields you would do it like so:

    
    add_filter('yikes-mailchimp-field-data', 'add_pattern_to_mailchimp_urls', 10, 3);
    
    function add_pattern_to_mailchimp_urls( $field_array, $field, $form_id  ) {
        if( isset( $field['merge'] ) && 'url' == $field['type'] ) {
            $field_array['pattern'] = 'pattern="^(http|https)?:\/\/([a-z0-9]+[.])?domain.com\/?$"';
        }
        return $field_array;
    }
    

    This will allow any subdomain as well as http or https for domain.com

    Thread Starter agreda

    (@agreda)

    Awesome! Thanks for the quick help.

    Will this affect all forms? If so, is it possible to target a specific form id?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Form Field Validation’ is closed to new replies.