Skip to Content
Menu

searchValidator()

Validate search terms for additional security and optimization.

JavaScript February 7, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

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/search.js on line 287.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.searchValidator = function(){
        //Wrap in requestIdleCallback when Safari supports it
        if ( jQuery('.input.search').length ){
            jQuery('.input.search').each(function(){
                if ( jQuery(this).val() === '' || jQuery(this).val().trim().length === 0 ){
                    jQuery(this).parent().children('.btn.submit').addClass('disallowed');
                } else {
                    jQuery(this).parent().children('.btn.submit').removeClass('disallowed').val('Search');
                    jQuery(this).parent().find('.input.search').removeClass('focusError');
                }
            });
    
            jQuery('.input.search').on('focus blur change keyup paste cut', function(e){
                let thisPlaceholder = ( jQuery(this).attr('data-prev-placeholder') !== 'undefined' )? jQuery(this).attr('data-prev-placeholder') : 'Search';
                if ( jQuery(this).val() === '' || jQuery(this).val().trim().length === 0 ){
                    jQuery(this).parent().children('.btn.submit').addClass('disallowed');
                    jQuery(this).parent().find('.btn.submit').val('Go');
                } else {
                    jQuery(this).parent().children('.btn.submit').removeClass('disallowed');
                    jQuery(this).parent().find('.input.search').removeClass('focusError').prop('title', '').attr('placeholder', thisPlaceholder);
                    jQuery(this).parent().find('.btn.submit').prop('title', '').removeClass('notallowed').val('Search');
                }
                if ( e.type === 'paste' ){
                    jQuery(this).parent().children('.btn.submit').removeClass('disallowed');
                    jQuery(this).parent().find('.input.search').prop('title', '').attr('placeholder', 'Search').removeClass('focusError');
                    jQuery(this).parent().find('.btn.submit').prop('title', '').removeClass('notallowed').val('Search');
                }
            });
    
            jQuery('form.search').on('submit', function(){
                if ( jQuery(this).find('.input.search').val() === '' || jQuery(this).find('.input.search').val().trim().length === 0 ){
                    jQuery(this).parent().find('.input.search').prop('title', 'Enter a valid search term.').attr('data-prev-placeholder', jQuery(this).attr('placeholder')).attr('placeholder', 'Enter a valid search term').addClass('focusError').trigger('focus').attr('value', '');
                    jQuery(this).parent().find('.btn.submit').prop('title', 'Enter a valid search term.').addClass('notallowed');
                    return false;
                }
    
                return true;
            });
        }
    };
    

    Override

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

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