Skip to Content
Menu

applyValidationClasses()

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

JavaScript February 7, 2021

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.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/forms.js on line 708.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.applyValidationClasses = async function($element, validation, showFeedback){
        //await nebula.yield();
    
        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)
        let parentElement = $element.parent();
        let feedbackElement = false;
        if ( $element.parent().find('.invalid-feedback').length ){
            parentElement = $element.parent();
            feedbackElement = $element.parent().find('.invalid-feedback');
        } else if ( $element.closest('.form-group, .form-check').find('.invalid-feedback').length ){
            parentElement = $element.closest('.form-group, .form-check');
            feedbackElement = $element.closest('.form-group, .form-check').find('.invalid-feedback');
        } else if ( $element.parents('.nebula-form-group').find('.invalid-feedback').length ){
            parentElement = $element.parents('.nebula-form-group');
            feedbackElement = $element.parents('.nebula-form-group').find('.invalid-feedback');
        }
    
        if ( feedbackElement ){
            if ( validation === 'feedback' || showFeedback ){
                feedbackElement.removeClass('hidden').show();
    
                if ( parentElement.find('.wpcf7-not-valid-tip').length ){
                    parentElement.find('.wpcf7-not-valid-tip').addClass('hidden'); //Hide the default CF7 message if we have a more helpful one for this field
                }
            } else {
                feedbackElement.addClass('hidden').hide();
            }
        }
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

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


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.applyValidationClasses = function(element, validation, showFeedback){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.applyValidationClasses === 'function' ){
            nebula.applyValidationClasses = function(element, validation, showFeedback){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });