Skip to Content

applyValidationClasses()

Add and remove the appropriate Bootstrap validation classes to the appropriate elements.

JavaScript April 30, 2018

Usage

JavaScript
applyValidationClasses(element, validation, showFeedback)

Parameters

element
(Required) (Object) The jQuery object of the input element itself
Default: None

validation
(Optional) (String) The type of validation to apply
Default: None

showFeedback
(Optional) (Boolean) Whether to show the form-control-feedback element
Default: false

Parameter Notes

Options for the validation parameter are “valid”, “invalid”, or “reset”. Reset simply removes all validation classes.

Request or provide clarification »

Examples

Add valid classes without showing feedback.

JavaScript
applyValidationClasses(jQuery(this), 'valid', false);

Add invalid classes and show feedback (if it exists).

JavaScript
applyValidationClasses(jQuery(this), 'invalid', true);

Show feedback without altering validation classes.

JavaScript
applyValidationClasses(jQuery(this), false, true);

Hide feedback without altering validation classes.

JavaScript
applyValidationClasses(jQuery(this), false, false);

Shortcut to show feedback without altering validation classes.

JavaScript
applyValidationClasses(jQuery(this), 'feedback');

Demo


Your name can not be empty.
Basic text validates as long as there is value.
Please check that you have entered a valid email address.
Please verify you have entered a valid URL.
Please verify you have entered a valid phone number.
Supports variations of country codes, area codes, delimiters, and even letters. Try adding an extension, too!
Please verify you have entered a valid date.
Supports both month day year and day month year formats. Also support written out days (with or without year).
Example shown as text input type, but works well with date input type too.
I'm sorry, the team you have selected is terrible.
Example of simple RegEx validation. With select menus, be sure the first option has value=""
#
Invalid HEX code.
Example of more complex RegEx validation.
Your message can not be empty.
Nebula removes validation when typing in textareas to avoid distractions.

Additional Notes

This function also removes Contact Form 7 validation classes (because CF7 classes are applied on submit of a form).

Sidenote: To validate required select menus, be sure the first option has value="".

Sidenote: Checkbox and radio validation in Bootstrap 4 only works with customized elements and as such is not included with Nebula live validation.

Source File

Located in /assets/js/nebula.js on line 2802.

JavaScript
nebula.applyValidationClasses = function(element, validation, showFeedback){
    if ( typeof element === 'string' ){
        element = jQuery(element);
    } else if ( typeof element !== 'object' ) {
        return false;
    }

    if ( validation === 'success' || validation === 'valid' ){
        element.removeClass('wpcf7-not-valid is-invalid').addClass('is-valid').parent().find('.wpcf7-not-valid-tip').remove();
    } else if ( validation === 'danger' || validation === 'error' || validation === 'invalid' ){
        element.removeClass('wpcf7-not-valid is-valid').addClass('is-invalid');
    } else if ( validation === 'reset' || validation === 'remove' ){
        element.removeClass('wpcf7-not-valid is-invalid is-valid').parent().find('.wpcf7-not-valid-tip').remove();
    }

    //Find the invalid feedback element (if it exists)
    var feedbackElement = false;
    if ( element.parent().find('.invalid-feedback').length ){
        feedbackElement = element.parent().find('.invalid-feedback');
    } else if ( element.closest('.form-group').find('.invalid-feedback').length ){
        feedbackElement = element.closest('.form-group').find('.invalid-feedback');
    }

    if ( feedbackElement ){
        if ( validation === 'feedback' || showFeedback ){
            feedbackElement.removeClass('hidden').show();
        } else {
            feedbackElement.addClass('hidden').hide();
            //element.removeClass('wpcf7-not-valid is-invalid is-valid').parent().find('.wpcf7-not-valid-tip').remove(); //What was this doing?
        }
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.applyValidationClasses = function(element, validation, showFeedback){
    //Write your own code here, leave it blank, or return false.
}