Skip to content

Instantly share code, notes, and snippets.

@miceno
Last active September 27, 2015 17:26
Show Gist options
  • Select an option

  • Save miceno/62bf4470c102cd82f2ed to your computer and use it in GitHub Desktop.

Select an option

Save miceno/62bf4470c102cd82f2ed to your computer and use it in GitHub Desktop.
Email confirmation on Wordpress Contact Form 7

This is a way to validate email by double email fields. Validation is via Javascript.

Files layout is:

../your-child-theme
    |- functions.php
    |- js/email-confirm.js
// First we trigger the form submit event
jQuery( document ).ready( function () {
jQuery('.wpcf7-submit').click(function () {
// We remove the error to avoid duplicate errors
jQuery('.error').remove();
// We create a variable to store our error message
var errorMsg = jQuery('<span class="error">Your emails do not match.</span>');
// Then we check our values to see if they match
// If they do not match we display the error and we do not allow form to submit
if (jQuery('.email').find('input').val() !== jQuery('.email-confirm').find('input').val()) {
errorMsg.insertAfter(jQuery('.email-confirm').find('input'));
return false;
} else {
// If they do match we remove the error and we submit the form
jQuery('.error').remove();
return true;
}
});
} );
<?php
//
// Hook for double check of email addresses.
// Taken from: http://stackoverflow.com/questions/6255367/contact-form-7-is-there-a-confirm-e-mail-input-type
//
function register_scripts() {
if ( !is_admin() ) {
// include your script
wp_enqueue_script( 'email-confirm', get_stylesheet_directory_uri() . '/js/email-confirm.js' );
}
}
add_action( 'wp_enqueue_scripts', 'register_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment