View Full Version : calculation within form before submit

05-24-2012, 05:47 PM
Hi Guys!

There are 3 fields in my form:
1. Start Date (datetime picker)
2. End Date (datetime picker)
3. Amount of Days (textbox)

I need to calculate the amount of days, based on Start Date input and End Date input.
Simply to say: Amount of days = End Date - Start Date
or: field3=field2-field1

Any suggestions for the respective code? I guess this should be javascript within the form code?
This calculation needs to be done before submit, so that the field3 is automatically filled in as soon as user picks the Start Date and End Date from the calendar within the form.

Below is the form code.
Thanks, Raivis

<div class="ccms_form_element cfdiv_datetime" id="start_date_container_div">
<label for="start_date">Select Start Date</label>
<input id="start_date" maxlength="150" size="16" class="cf_date_picker" title="" type="text" value="" name="start_date" />

<div class="clear"></div><div id="error-message-start_date"></div></div>

<div class="ccms_form_element cfdiv_datetime" id="finish_date_container_div">
<label for="finish_date">Select Finish Date</label>
<input id="finish_date" maxlength="150" size="16" class="cf_date_picker" title="" type="text" value="" name="finish_date" />

<div class="clear"></div><div id="error-message-finish_date"></div></div>

<div class="ccms_form_element cfdiv_text" id="total_days_container_div">
<label for="total_days">Total Days</label>
<input id="total_days" maxlength="150" size="10" class=" validate['required','number']" title="" type="text" value="" name="total_days" />

<div class="clear"></div><div id="error-message-total_days"></div></div>

05-24-2012, 06:00 PM
you can swap the date strings for the values generated by your date picker:

start=new Date ("12 Jun 2012")
end=new Date ("Aug 13 2012")
alert (diff + " days difference")