Calculation script using a form
Hello,
I am currently looking to build a form for a website with calculation/conditions in it... I am not a big coder, so if someone can give me the solution or can code this for me (with remuneration of course - if so tell me how much you would like before starting the job).
I enclose to my message a link to an excel sheet showing what is basically needed to be done.
http://wtrns.fr/s1fWWBPO1gBKJpo
Thank you in advance for your feedback.
Code:
<html>
<body>
Years of work: <input type="text" name="years_of_work" id="years_of_work" value="" onchange="javascript :calcProtectionTime();"/><br/>
Begin work incapacity: <input type="text" name="begin_date_incapacity" id="begin_date_incapacity" value="" onchange="javascript :calcEndProtection();"/><br/>
Protection Time: <input type="text" name="protection_time" id="protection_time" readonly /><br/>
End of Protection: <input type="text" name="end_of_protection" id="end_of_protection" readonly><br/>
<a href="javascript :void(0);" onclick="javascript :calculate();">Calculate</a>
<script>
function calculate() {
var yrsWork = parseFloat(document.getElementById('years_of_work').value);
var incapacityStart = new Date(document.getElementById('begin_date_incapacity').value);
var protTime = 0;
if (yrsWork>9) {
protTime = 720;
} else if (yrsWork>5) {
protTime = 180;
} else if (yrsWork>1) {
protTime = 112;
} else if (yrsWork>0) {
protTime = 56;
} else {
protTime = 0;
}
if (!(incapacityStart == 'Invalid Date')) {
var endDate = new Date(incapacityStart.getFullYear(), incapacityStart.getMonth(), incapacityStart.getDate() + protTime);
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var curr_date = endDate.getDate();
var curr_month = endDate.getMonth();
var curr_year = endDate.getFullYear();
var strEndDate = m_names[curr_month] + ' ' + curr_date + ', ' + curr_year;
} else {
var strEndDate = "results";
}
//update page contents
document.getElementById('protection_time').value = protTime;
document.getElementById('end_of_protection').value = strEndDate;
}
function calcProtectionTime() {
var yrsWork = parseFloat(document.getElementById('years_of_work').value);
if (yrsWork>9) {
protTime = 720;
} else if (yrsWork>5) {
protTime = 180;
} else if (yrsWork>1) {
protTime = 112;
} else if (yrsWork>0) {
protTime = 56;
} else {
protTime = 0;
}
document.getElementById('protection_time').value = protTime;
calcEndProtection();
}
function calcEndProtection() {
var incapacityStart = new Date(document.getElementById('begin_date_incapacity').value);
var protTime = parseFloat(document.getElementById('protection_time').value)
if (!(incapacityStart == 'Invalid Date')) {
var endDate = new Date(incapacityStart.getFullYear(), incapacityStart.getMonth(), incapacityStart.getDate() + protTime);
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var curr_date = endDate.getDate();
var curr_month = endDate.getMonth();
var curr_year = endDate.getFullYear();
if (!(m_names[curr_month]) || isNaN(curr_date) || isNaN(curr_year)) {
var strEndDate = "results";
} else {
var strEndDate = m_names[curr_month] + ' ' + curr_date + ', ' + curr_year;
}
} else {
var strEndDate = "results";
}
document.getElementById('end_of_protection').value = strEndDate;
}
</script>
</body>
</html>
An other page to compare ...
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>Untitled</title>
<style type="text/css">
body {font-family:Garamond;margin:0;padding:0;font-size:16px;}
form.css {display:block;width:430px;background-color:#cccccc;margin:20px;padding:5px;line-height:20px}
form.css b {font-weight:1200}
form.css fieldset{padding:7px;}
form.css label {display:inline;float:left;width:260px;}
form.css input {display:inline;float:left;width:130px;height:18px;margin:0;text-align:center;}
form.css input.rsl {border:1px solid black}
</style>
<script type="text/javascript">
function calcTime(v){var n=Number(v),j=0,s;
if (isNaN(n)) {alert("Not valid value !");return}
if (9<n) j=720;
else if (5<n) j=180;
else if (1<n) j=112;
else if (0<n) j=56;
document.getElementById('prtTme').value=j;
if (s=document.getElementById('bgnInc').value) calcDate(s);
}
Number.prototype.twoDigits=function(){var t=this.toString();while (t.length<2) t='0'+t;return t}
function calcDate(v){var d=m=y=0;
v.replace(/\0?(\d+).0?(\d+).(\d\d\d\d)/,function(n,o,p,q){d=o;m=p-1;y=q});
dte=new Date(y,m,d,12,0,0);
if (d!=dte.getDate() || m!=dte.getMonth() || y!=dte.getFullYear()) {alert("Not valid date !");return}
j=Number(document.getElementById('yeaWrk').value);
if (isNaN(j) || j==0) {alert("Please, fill the years of Work !");return}
edt=new Date(y,m,d+j,12,0,0);
document.getElementById('endTme').value=edt.getDate().twoDigits()+' '+(edt.getMonth()+1).twoDigits()+' '+edt.getFullYear();
}
</script>
</head>
<body>
<form id="frm" class="css">
<fieldset><legend>Informations</legend>
<label for="lstNme"><b>Last name</b></label><input type="text" id="lstNme" name="lstName" value=""><br>
<label for="frsNme"><b>Fisrt name</b></label><input type="text" id="frsNme" name="frsName" value=""><br>
<label for="nmb"><b>Number</b></label><input type="text" id="nmb" name="nmb" value=""><br>
<label for="yeaWrk"><b>Years of work</b> (number) </label><input type="text" id="yeaWrk" name="yeaWrk" value="" onblur="calcTime(this.value)"><br>
<label for="bgnInc"><b>Beginnig of incapacity</b> (dd mm aaaa) </label><input type="text" id="bgnInc" name="bgnInc" value="" onblur="calcDate(this.value)"><br>
</fieldset>
<fieldset><legend>Résultats</legend>
<label for="prtTme">Protection time</label><input type="text" class="rsl" id="prtTme" name="prtTme" readonly id="prtTme"><br>
<label for="endTme">End of protection</label><input type="text" class="rsl" id="endTme" name="endTme" readonly id="endTme"><br>
</fieldset>
</form>
</body>
</html>
Last edited by 007Julien; 01-24-2012 at 11:11 AM .
Cannot make it work
Hi Nap0leon,
Thank you for your work. I was hoping for a quick answer without expecting it really...
I gave a try to your script and I couldn't make it work.
When I click on the "claculate" link I got an error message :
"The requested URL .../java_script:void(0); was not found on this server."
Do you know what this could come from ?
Hi,
Thank you for this. It's almost perfect. But the calculation seems wrong...
On my excel spreadsheet when I type :
Years of work : 5
Begining ... : 01 01 2012
The results are
Protection time : 112
End of protection : 22 04 2012 (your script is saying : 15 01 2012)
Can you let me know what should be corrected in order to make it right ?
Sorry, replace the calcDate function with this code
Code:
function calcDate(v){var d=m=y=0;
v.replace(/\0?(\d+).0?(\d+).(\d\d\d\d)/,function(n,o,p,q){d=o;m=p-1;y=q});
dte=new Date(y,m,d,12,0,0);
if (d!=dte.getDate() || m!=dte.getMonth() || y!=dte.getFullYear()) {alert("Not valid date !");return}
j=Number(document.getElementById('yeaWrk').value);
if (isNaN(j) || j==0) {alert("Please, fill the years of Work !");return}
j=Number(document.getElementById('prtTme').value);
edt=new Date(y,m,d-(-j),12,0,0);
document.getElementById('endTme').value=edt.getDate().twoDigits()+' '+(edt.getMonth()+1).twoDigits()+' '+edt.getFullYear();
}
I confused the years of incapacity and in the daytime of continuation ! Besides javascript concatenate the values instead of adding them...
Last edited by 007Julien; 01-24-2012 at 11:32 AM .
Thank you Julien. Works very well now ! You seem very efficient :-) I am owning a small web design agency.
Is there a better way to contact you in future if I have some similar requests (e-mail, skype) ?
As every work deserve money, what would be your price for this ? I could make a payment with paypal if this would be convenient for you ?
The following variant pattern will accepts all non digit separators (like one or more spaces, tabs, or other commas, points...) and day or month without leading zero. ( 1 1 2010 is now readable like 01.01.2010 or 1/1/2010).
Code:
function calcDate(v){var d=m=y=0;
v.replace(/\0?(\d+)[^\d]+0?(\d+)[^\d]+(\d\d\d\d)/,function(n,o,p,q){d=o;m=p-1;y=q});
dte=new Date(y,m,d,12,0,0);
//...
}
Thanks again for your work (even better now). I'll give a try tomorrow (11.35 pm here now).
Also as said before let me know if this forum is the correct way to contact you or if we can communicate with e-mail or maybe Skype. If you want you can send me your details here : aimejee@me.com
You can send private message in this forum (with a click on the names). I work only for conveniences and pleaser...
Thanks for your reply, I am new to the forum and saw about the private message a bit too late. :-)
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks