8
if ($('#requestForm').val == "") {
alert('Please select at least one filter');
    //return false;
}
else { 
    ///Run my code
}

I have a form, no inputs are required, but if no inputs are selected I want an alert to tell them to select at least one option.

gdoron
  • 147,333
  • 58
  • 291
  • 367
user1253239
  • 199
  • 2
  • 3
  • 13

5 Answers5

8
var validate= false;
$('#requestForm input').each(function(){
    if($(this).val() != '' || $(this).attr('checked'))
        validate = true;
});
if(!validate){
    alert('Please select at least one filter');
    return false;
}
else { 
    Run my code
}
Dattatray Walunj
  • 175
  • 1
  • 10
  • 2
    You over use jQuery `this.value` and `this.checked` are much better. – gdoron May 02 '12 at 12:41
  • This is the one I used, to make it more clear what I am doing I have a form that provides search of records. I can't require any of the fields cause they can use one, or two or three or four or 8 fields to filter the data to find their record. But just in case someone hits the submit button without selecting any inputs, I notify them to at least select one filter for the search. – user1253239 May 02 '12 at 12:54
5
var isValid = !!$('form :input').filter(function() {
    return this.value;
}).length;​

if (!isValid)
    alert('Please select at least one filter');

If the form id is requestForm use it as the form selector:

$('#requestForm :input').filter(function() {
    ...
gdoron
  • 147,333
  • 58
  • 291
  • 367
2

Neither of the current answers benefit from short circuiting as soon the condition is met. This probably only amounts to a marginal hit on performance for a finite number of fields in JavaScript. But also, neither are great at declaratively stating the intention of the loop you're entering.

Here's a simple 4 line extension method that takes in any filter criteria and returns true as soon as something matches that criteria.

jQuery.fn.any = function(filter){ 
    for (i=0 ; i<this.length ; i++) {
     if (filter.call(this[i])) return true;
  }
  return false;
};

Then use it like this

var gotMatch = $(":input").any(function() { 
                   // you can put any condition you want in here
                   return this.value == 'hi';
               });

jQuery.fn.any = function(filter){ 
 for (i=0 ; i<this.length ; i++) {
    if (filter.call(this[i])) return true;
  }
  return false;
};

$(function() {
 
  var gotMatch = $(":input").any(function() { 
                   return this.value == 'hi';
                 });

  if (gotMatch) {
    console.log("Hello to you too!");
  } else {
   console.log("Why don't you say Hi!");
  }
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" value="">
<input type="text" value="">
<input type="text" value="">

Demo in jsFiddle

Further Reading:

Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664
0

It's not so clear to me what you're trying to achieve but maybe you can find useful this

https://github.com/maxatwork/form2js

It's a library that converts structured form data into Javascript objects. You only have to do this

var obj = form2js("requestForm");

and then parse the result and warn the user if nothing has been selected.

ab_732
  • 3,639
  • 6
  • 45
  • 61
0
check = function()
    {                
var empty = $("#requestForm :input").filter(function() {
return this.value == ""; });
if(empty.length) {
alert("Please select at least one filter");
};}

This will alert the message ONCE if at least one input is empty. then simply add the function onclick to your submit input or any button you want to validate the form with.

<input type="submit" onclick="check();">
Kareem
  • 5,068
  • 44
  • 38