Skip to Content

searchValidator()

Validate search terms for additional security and optimization.

JavaScript February 22, 2017

Usage

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

Source File

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

JavaScript
nebula.searchValidator = function(){
    //Wrap in requestIdleCallback once fully supported (IE11 and Safari)
    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){
            var 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;
            } else {
                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.
}