Reputation: 175
I have 3 elements with different IDs. On click of a submit button, I should validate whether any one of the fields is filled with data. If none of them are filled, suitable message should be shown to the user. If any one of them is filled, the form should be submitted.
Though they belong to the same class, I will not be able to use class selectors as there are other fields with the same class for which this validation does not apply.
I have done the following but does not work
if( $("#id1,#id2,#id3").val() == "")
-->> this uses OR operator and I am looking for AND
if( $("#id1#id2#id3").val() == "")
--> does not work
if( $("#id1[#id2][#id3]").val() == "")
--> neither does this
Upvotes: 0
Views: 1063
Reputation: 10479
Personally I'd use the each
method
var $empty = false;
$("#id1,#id2,#id3").each(function(i, e){
if ($(e).val() == '') $empty = true;
});
if ($empty)
{
alert("Please enter some data.");
}
Upvotes: 2
Reputation: 1247
Look into jQuery add(), you're also going to need to loop through all of them to check for each one, so for your example:
var valid = true;
$("#id1").add("#id2").add("id3").each(function() {
if ($(this).val() == "")
valid = false;
});
Upvotes: 2