Skip to content

Instantly share code, notes, and snippets.

@kubido
Created October 31, 2011 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubido/a630cb8961351024cf15 to your computer and use it in GitHub Desktop.
Save kubido/a630cb8961351024cf15 to your computer and use it in GitHub Desktop.
add client validation for search form if it's input is null
$('form[action*="search"]').live('submit', function(event) {
event.preventDefault();
arr_input = $('form').serializeArray();
arr_input.shift();
keywords_count = 0;
$.map(arr_input, function(el){
if(el.value != ""){keywords_count = keywords_count + 1}
});
if(keywords_count <= 0){
if($('.error.empty-search').length < 1){
error_el = '<div class="alert-message error empty-search" style="display:none" data-alert="alert"><a class="close" href="#">×</a>Enter at least one search term to perform any search</div>'
$('.breadcrumb').append(error_el)
$('.error.empty-search').fadeIn('slow');
}
}else{
$(this).attr({
'data-pjax': '#content',
'data-pjax-fragment': '#content'
});
$('form').submit()
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment