Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Created June 21, 2016 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfeck/86a93df2cddfa7efba7427e66ab0b22b to your computer and use it in GitHub Desktop.
Save joshfeck/86a93df2cddfa7efba7427e66ab0b22b to your computer and use it in GitHub Desktop.
Rough and tumble quick and dirty example of how to add some custom JavaScript validation to an EE4 form field.
// add script to registration form
add_action( 'AHEE__attendee_information__reg_step_start', 'eea_custom_validation' );
function eea_custom_validation() {
?>
<script>
jQuery(document).ready(function($) {
$('.ee-reg-qstn-78').keyup(function() { // use your unique input class
$('span.error-keyup-78').remove();
var inputVal = $(this).val();
var characterReg = /^\s*[a-zA-Z0-9,\s]+\s*$/; // your custom regex, this checks for special characters
if(!characterReg.test(inputVal)) {
$(this).after('<span class="error ee-required-text error-keyup-78">Only a-z and 0-9 please.</span>');
}
});
// handle the submit button
$('#spco-go-to-step-finalize_registration-submit').click(function() {
if($('span.error').length > 0){
alert('Please correct the errors in the form.');
return false;
}
});
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment