Skip to content

Instantly share code, notes, and snippets.

@corradomatt
Forked from guyroutledge/inline-validation.js
Last active August 29, 2015 14:24
Show Gist options
  • Save corradomatt/aa919d70bf222e909332 to your computer and use it in GitHub Desktop.
Save corradomatt/aa919d70bf222e909332 to your computer and use it in GitHub Desktop.
// if an invalid form field has been made valid,
// remove the shouty error highlighting - if a valid
// required field has been made invalid, start shouting
$('input, textarea, select').on('change', function(){
var $input = $(this);
var isRequired = $input.parents('.gfield').is('.gfield_contains_required');
var isValid = $input.is(':valid');
if ( isRequired && isValid ) {
$input.parents('.gfield').removeClass('gfield_error');
$input.parent().next('.validation_message').slideUp();
}
}).blur(function(){
var $input = $(this);
var isRequired = $input.parents('.gfield').is('.gfield_contains_required');
var isInValid = $input.is(':invalid');
var isEmpty = $input.val() === '';
if ( isRequired && ( isInValid || isEmpty ) ) {
$input.parents('.gfield').addClass('gfield_error');
$input.parent().next('.validation_message').slideDown();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment