views:

727

answers:

4

I've got multiple input fields where they have the same class names. I need to check to make sure at least 1 of them isn't empty. So I've got,

$('form[name=expForm]').submit(function(){
 var msg = "";
 var found = false;
 $('.date-mask').each(function(){
  if($(this).val()){
   found = true;
  }
 });

 if (found != true)  {

msg += "Please provide at least 1 date before submitting.\n"; return false; } var calcFnd = false; $('.calc').each(function(){ if($(this).val()){ calcFnd = true; } });

if (calcFnd != true) { msg += "Please provide at least 1 expense before submitting.\n"; return false; }

if(msg != ""){ alert(msg); return false; }

 if($('.ttlR27').val()==""){
  var net = $('.ttlR26').val();
  $('.ttlR28').val(net);
 }
 return false;

});

<cfloop from="1" to="#ArrayLen(labels)#" index="r">
<tr>
 <td class="labels <cfif labels[r] EQ "Day of Week:">row1</cfif>"><cfif ArrayIsDefined(labels,r) AND labels[r] NEQ "Open1"><cfif labels[r] EQ "Open"><input type="text" id="descript#r#" name="descript#r#" class="description descript#r#" value="Enter text here" style="width:auto;" /><cfelse>#labels[r]#</cfif></cfif></td>   

 <cfloop from="1" to="7" index="i">
  <td id="Day#i#" class="row#r# col#i#">
   <cfif r EQ 1>#Left(DayOfWeekAsString(i),3)#<cfelse><cfif r EQ 2>
   <input type="text" class="date-mask" name="dates#i#" required="yes" message="Please provide at least 1 date before submitting.">
   <cfelse>
   <input type="text" 
   <cfif labels[r] EQ "Personal Car: Mileage ##"> id="gasamount#i#" <cfelseif labels[r] EQ "Personal Car: Mileage $">id="gasmoney#i#"</cfif><cfif labels[r] EQ "Open">id="open#r#"</cfif><cfif labels[r] EQ "Daily Totals">id="dailytotals#i#"</cfif> class="all <cfif labels[r] EQ "Personal Car: Mileage ##">gasamount <cfelse><cfif labels[r] NEQ "Daily Totals">C#i# </cfif></cfif><cfif labels[r] EQ "Personal Car: Mileage $">gasmoney<cfelse>calc R#r#<cfif labels[r] EQ "Daily Totals"> </cfif></cfif><cfif labels[r] EQ "Daily Totals">ttlC#i#</cfif><cfif labels[r] EQ "Less Advance(if applicable)"> less</cfif><cfif labels[r] EQ "Net Due Employee"> net</cfif><cfif labels[r] EQ "Open"> open</cfif>" 
    <cfif labels[r] EQ "Daily Totals" OR labels[r] EQ "Personal Car: Mileage $" OR labels[r] EQ "Open1">readonly="readonly"</cfif>
    name="<cfif labels[r] NEQ "Personal Car: Mileage ##" AND labels[r] NEQ "Personal Car: Mileage $" AND labels[r] NEQ "Dates:" AND labels[r] NEQ "Open1" AND labels[r] NEQ "Daily Totals">R#r#.C#i#</cfif><cfif labels[r] EQ "Personal Car: Mileage ##">gasamt#i#</cfif><cfif labels[r] EQ "Daily Totals">celltotals#i#</cfif><cfif labels[r] EQ "Personal Car: Mileage $">gastot#i#</cfif>" /></cfif>
      </cfif>
  </td>
 </cfloop>

  <td class="totals<cfif r EQ 1>1</cfif>"><cfif r EQ 1>Total<cfelse><input type="text" <cfif labels[r] EQ "Less Advance(if applicable)">id="less"</cfif><cfif labels[r] EQ "Net Due Employee">id="net"</cfif>id="totals" class="ttlR#r#" name="totals#r#" readonly="readonly" /></cfif></td>


</tr>
</cfloop>

The class names that are being used are: date-mask, for the top row of dates and calc, for the rest of the table.

I only need 1 input to not be empty to allow for a true submission.

Any ideas?

Edit Here is the link to the live page. What I'm validating here are two different things, but essentially the same function. The first row, the dates. Also, the entire table. If not all, MOSTLY every input has one of the same classes.

A: 

You could have a validation flag you set in your code and submit if it's true.

var valid = false;
$('.huh').each(function(){
  var value = $(this).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  if(value.length > 0){
    valid = true;
    return false;
  }
});

The first statement in the each will trim the whitespaces and then check for the length of the value. If it's greater than 0 we have a value; set the valid flag to true and return false to stop the iterations.

Doomspork
you do realize you're going to get the same result..
Michael Stone
Same result? Care to elaborate?
Doomspork
I've double checked the code and tested it, I'm getting the correct result. Can you explain why you downvoted?
Doomspork
I return 1 alert rather than a list of them all. I still return false for having a true statement. Does not work. That's what I mean.
Michael Stone
I'm not at 100 reputation points.. I can't downvote.. Thanks though.
Michael Stone
I guess there is a secret meaning to your question I'm missing! You want to make sure at least one isn't empty, that's what that code does. Using the 5 inputs you provided, valid is true if one or more of them have a value
Doomspork
Check the link I've posted. That should be able to give you more input. Thanks for trying to help btw.
Michael Stone
I've looked at your link, can you elaborate a little more on what you're trying to accomplish? Based on the other responses I think the question may be unclear. Glad to help :)
Doomspork
It's still the same concept. The date fields need at least 1 out of the 7 fields not empty. The rest of the table has multiple classes, but they all have a universal connector class called, calc. I need at least 1 input box with the class name of calc and date-mask (date fields class) to have some sort of value.
Michael Stone
A: 

You could do something like this:

var valid = !!$.makeArray($('input.huh').map(function() { return this.value; })).join('');

Explanation:

Get all the inputs, map the array to just get the values, change the array to a JS array and join. If at least one has a value, the joined string will not be empty.

The !! just converts to a boolean (may or may not be necessary/desirable).

Ben
I really don't even want to attempt yours at the moment. It's a bit much for something like this. If all else fails, I'll give it a shot.
Michael Stone
Not sure what you mean... it's one line of code to get a boolean indicating whether you have any inputs with values.This is just a more functional-oriented version of the loop in the accepted answer.
Ben
+1  A: 

Here:

var found = false;
$('.huh').each(function(){
   if($(this).val()){
      found = true;
      return false;
   }
});
karim79
That doesn't even work for this. No errors, but no results. Check the link I've posted. The table is... pretty large, but it's consisted of .calc for everything except for the dates. The dates consist of .date-mask
Michael Stone
@Michael, you know that you're supposed to use the `found` variable after the loop, right?
Randell
I think this might work. I had it after the loop in the first place though..
Michael Stone
eh. No. Didn't work.
Michael Stone
+1  A: 

@karim79's code should work. Here, I wrote a proof of concept that it works:

The script:

function checkFields() {
    var found = false;
    $('.huh').each(function(){
        if($(this).val()){
            found = true;
            return false;
        }
    });

    if (found == true)  {
        alert("at least one field has value");
    } else {
        alert("all fields are empty");
    }
}

The mark-up

<input type="text" class="huh" name="limit1" />
<input type="text" class="huh" name="limit2" />
<input type="text" class="huh" name="limit3" />
<input type="text" class="huh" name="limit4" />
<input type="text" class="huh" name="limit5" />
<input type="button" name="submit" value="Submit" onclick="checkFields();"/>

Try this code yourself on a clean html and you'll see that it actually works.

Edit for the added code: The code is not reaching the alert part because you are already returning false here: msg += "Please provide at least 1 date before submitting.\n"; return false; Don't return until you've shown the alert

Randell
Was working. I'll edit my code to give exactly what I've got.
Michael Stone
I tried to not have to supply the original code because of the coldfusion involved. If you check the link, you'll see that I am using the code shown in your example. Does not return a message. Thus, not working. I put a return false at the end of the form submit, for debugging purposes.
Michael Stone
The code is not reaching the `alert` part because you are already returning `false` here: `msg += "Please provide at least 1 date before submitting.\n"; return false;` Don't return until you've shown the alert.
Randell
yup. that was it. pretty sure it's working now.
Michael Stone
Awesome. I can sleep now. =D
Randell
LMAO Thank you sir!
Michael Stone