Skip to content

Instantly share code, notes, and snippets.

@Apina
Created September 29, 2014 10:34
Show Gist options
  • Save Apina/bc1b19ff2f3c3b73b369 to your computer and use it in GitHub Desktop.
Save Apina/bc1b19ff2f3c3b73b369 to your computer and use it in GitHub Desktop.
EE4 US Phone and Zip validation: This needs to be added to a custom functions plugin or similar. It is very basic, for instance it loads on every page rather than just EE related pages, but should provide a basis for allowing validation. Also note that the addClass required isn't strictly necessary but may be useful.
function dr_validate() {
wp_register_script( 'drvalid', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js', array('jquery'), '1.13.0', true );
wp_register_script( 'drvalid2', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.min.js', array('jquery'), '1.13.0', true );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'drvalid' );
wp_enqueue_script( 'drvalid2' );
}
add_action('wp_enqueue_scripts','dr_validate');
function dr_validate_zip_phone() {
?>
<script type="text/javascript">
jQuery( document ).ready(function() {
jQuery("#spco-registration-attendee_information-frm").validate();
jQuery(".phone").rules("add", {
required: true,
phoneUS: true,
required: true
}).addClass('required');
jQuery(".zip").rules("add", {
required: true,
digits: true,
minlength: 5,
maxlength: 5
});
}).addClass('required');
</script>
<?php
}
add_action('wp_footer', 'dr_validate_zip_phone');
@lorenzocaum
Copy link

function dr_validate() {

global $load_espresso_scripts;

  if ( ! $load_espresso_scripts )
    return;

    wp_register_script( 'drvalid', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js', array('jquery'), '1.13.0', true );
    wp_register_script( 'drvalid2', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.min.js', array('jquery'), '1.13.0', true );

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'drvalid' );
    wp_enqueue_script( 'drvalid2' );
}
add_action('wp_enqueue_scripts','dr_validate');

function dr_validate_zip_phone() {

global $load_espresso_scripts;

  if ( ! $load_espresso_scripts )
    return;

?>
    <script type="text/javascript">

        jQuery( document ).ready(function() {

            jQuery("#spco-registration-attendee_information-frm").validate();

            jQuery(".phone").rules("add", { 
                required: true,
                phoneUS: true,
                required: true
            }).addClass('required');

            jQuery(".zip").rules("add", { 
                required: true,
                digits: true,
                minlength: 5,
                maxlength: 5
            });

        }).addClass('required');

    </script>

<?php
}
add_action('wp_footer', 'dr_validate_zip_phone');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment