user1253239
user1253239

Reputation: 199

Jquery check form to see if any inputs has value

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.

Upvotes: 8

Views: 22592

Answers (5)

KyleMit
KyleMit

Reputation: 29839

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:

Upvotes: 2

Kareem
Kareem

Reputation: 5404

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();">

Upvotes: 0

Dattatray Walunj
Dattatray Walunj

Reputation: 175

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
}

Upvotes: 8

ab_732
ab_732

Reputation: 3887

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.

Upvotes: 0

gdoron
gdoron

Reputation: 150253

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() {
    ...

Upvotes: 5

Related Questions