Skip to content

Instantly share code, notes, and snippets.

@musamamasood
Created December 7, 2016 03:39
Show Gist options
  • Save musamamasood/bf907195fe980b8ba6b316b2a27b3c28 to your computer and use it in GitHub Desktop.
Save musamamasood/bf907195fe980b8ba6b316b2a27b3c28 to your computer and use it in GitHub Desktop.
jQuery plugin to prevent the empty search.
/**
* Stop empty searches
*
* @author Thomas Scholz http://toscho.de
* @param $ jQuery object
* @return bool|object
*/
(function( $ ) {
$.fn.preventEmptySubmit = function( options ) {
var settings = {
inputselector: "#s",
msg : "Don’t waste your time with an empty search!"
};
if ( options ) {
$.extend( settings, options );
};
this.submit( function() {
var s = $( this ).find( settings.inputselector );
if ( ! s.val() ) {
alert( settings.msg );
s.focus();
return false;
}
return true;
});
return this;
};
})( jQuery );
/*
*
* functions to call after include above plugin.
*/
jQuery(document).ready( function( $ ){
$( ".search_form" ).preventEmptySubmit({ /* search form selector */
inputselector: "#search-text" /* search input selector.*/
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment