Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Require at least 1 interest grouping choice to be selected in a MailChimp for WordPress form.
<p>
<label for="mc4wp_email">Email address: </label>
<input type="email" name="EMAIL" required placeholder="Your email.." />
</p>
<p>
<label>Checkbox Groups</label>
<label>
<input name="INTERESTS[c503c0f57d][]" type="checkbox" value="0f8a0ed3bd"> <span>Group 1</span>
</label>
<label>
<input name="INTERESTS[c503c0f57d][]" type="checkbox" value="9446882237" checked> <span>Group 2</span>
</label>
<label>
<input name="INTERESTS[c503c0f57d][]" type="checkbox" value="c12429b8e8"> <span>Group 3</span>
</label>
</p>
<p>
<input type="submit" value="Subscribe" />
</p>
<script>
jQuery('.mc4wp-form').find('form').on('submit', function(evt) {
var checkboxFields = jQuery(this).find('[name^="INTERESTS"]');
if( checkboxFields.length ) {
// check if at least one field is required
for(var i=0; i<checkboxFields.length; i++) {
if( checkboxFields[i].checked ) {
return true;
}
}
alert("Please select at least 1 group.");
evt.preventDefault;
return false;
}
return true;
});
</script>
@carlosvarela

This comment has been minimized.

Copy link

commented Jun 9, 2015

Great code!.

just to update. Today is "group" rather than "GROUPINGS"

@ENBertussi

This comment has been minimized.

Copy link

commented Jul 11, 2018

This is great stuff, but how would this be achieved with radio buttons?

this does not seem to be pulling it off integrating with CF7

<p>
    <label>Role</label>
    <label>
        <input name="INTERESTS[XXXXXXXXXX]" type="radio" value="XXXXXXXXXX" required=""> <span>Tech Professional</span>
    </label>
    <label>
        <input name="INTERESTS[XXXXXXXXXX]" type="radio" value="XXXXXXXXXX" required=""> <span>L&amp;D Professional</span>
    </label>
</p>

Thanks for any tips you can provide.

cheers..

ENB..//

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.