I asked a question about totalling several fields at the end of a row of
fields using JS on the CF Talk list!

Nate Smith has been helping me through the day work out a cool function
where you can pass the Field name (or in this case the first three
letters of the field category) and then the field you want to display
the totals in, add this to an OnBlur event and VOILA! 

Here is the javascript:

<SCRIPT LANGUAGE="JavaScript">

function sumValues(SearchString,DisplayElement)
{
        var TheSum = 0;
        var RE = new RegExp();
        
        // dynamically reference the display field
        var TheDisplayElement = eval('document.Timeentry_billable.' +
DisplayElement);
        // set the Regular Expression search string to the passed in
string
        RE = eval('/^'+SearchString+'/');
        
        // loop through the form looking for the string
        for( var i=0; i<document.Timeentry_billable.elements.length;
i++)
        {
        
if(document.Timeentry_billable.elements[i].name.search(RE)
!= -1) 
                {
                        // parse the numberic data
                        TheVal =
parseFloat(document.Timeentry_billable.elements[i].value);
                        // if the value is numeric, add
                        if( !isNaN(TheVal) ) {
                                TheSum += TheVal;
                        }
                        // if the value is not numeric and has more than
one
char, display an alert
                        else if( isNaN(TheVal) &&
document.Timeentry_billable.elements[i].value.length > 0 ) {
                                alert('Text is not allowed');
        
document.Timeentry_billable.elements[i].value = '';
                                break;
                        }
                }
        }
        
        // push the value into the DisplayElement field
        TheDisplayElement.value = TheSum;
}
</SCRIPT>


It even contains validation testing that a character is not entered into
the number field.

The fields are created with the following code:

        <cfquery name="IsHoliday" datasource="xxxx" dbtype="Oracle80">

                Select Holiday_Desc From Holiday_TBL

                Where Holiday_Date =
to_date('#DateFormat(currentdate,'mm/dd/yyyy')#','mm/dd/yyyy')

        </cfquery> 

<CFIF IsHoliday.RecordCount neq 0>

        <td align="center" valign="middle" bgcolor="#9999FF"
class="smalltext"><CFOUTPUT><input type="text"
name="Reg_#DateFormat(CurrentDate,'mmddyyyy')#" size = "5" value="0"
class="boxes" OnBlur="sumValues('Reg','regtotals')"> </CFOUTPUT></td>
        
<CFELSEIF IsWeekend(#CurrentDate#)>
                
        <td align="center" valign="middle" bgcolor="#FFCC99"
class="smalltext"><CFOUTPUT><input type="text"
name="Reg_#DateFormat(CurrentDate,'mmddyyyy')#" size = "5" value="0"
class="boxes" OnBlur="sumValues('Reg','regtotals')"> </CFOUTPUT></td>
                                
<CFELSE>
                
        <td align="center" valign="middle" bgcolor="#999999"
class="smalltext"><CFOUTPUT><input type="text"
name="Reg_#DateFormat(CurrentDate,'mmddyyyy')#" size = "5" value="0"
class="boxes" OnBlur="sumValues('Reg','regtotals')"> </CFOUTPUT></td>
</CFIF>

        <CFSET CurrentDate = DateAdd("d",CurrentDate,1)><!--- Advance
current Date one day --->

</CFLOOP>
                  <td width="46" align="center" valign="middle"
bgcolor="#999999"> 

                    <input type="text" name="regtotals" size="6"
maxlength="6" class="boxes">

                  </td>

                </tr>
<!--- End Regular Hours Section --->


All hail Nate! 

^_^

Someone give him a muffin if he's subscribed to the list..I'm *munch*
*munch* all outff...

-Gel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to