Override Clientside Validation email implementation to support the old format: Drupal

Last updated on 03/03/2017
/**
 * Implements hook_ctools_plugin_pre_alter().
 *
 * We want to override the  email validation because "jQuery Validate Plugin"
 * used by clientside_validation" is using the same email validation as
 * HTML standard does which matches something@something as valid.
 * We are not ready to relax this rule and want to require the
 * something@something.something structure.
 *
 * @see: https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
 *            http://stackoverflow.com/a/28942158/1600028
 *
 * @param $plugin
 * @param $info
 */
function hook_ctools_plugin_pre_alter(&$plugin, &$info) {
 
  // Override the email JS rule of clientside_validation.
  if ($info['module'] == 'clientside_validation') {
    if($plugin['validator']['class'] == 'CvFAPIEmailValidator') {
      $plugin['validator']['constructor argument']['js_rule'] = 'customEmail';
    }
  }
}

The JS rule itself

$(document).bind('clientsideValidationAddCustomRules', function (event) {
  $.validator.addMethod("customEmail", function (value, element, param) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    var valid = re.test(value);
 
    // Also ensure we check if it's required.
    return this.optional(element) || valid;
  });
});