Solved

JavaScript Code Help

Posted on 2011-03-02
4
332 Views
Last Modified: 2012-05-11
I attached some javascript code I am working on. If I type in a date in the "Bill Date" field & click the calculate button a date that is 7 days greater than the bill date is autopopluated into the due date field. The "inventory date"field is supposed to be autopopulated 14 days greater than the bill date field when the calculate button is clicked. I am getting the nanana instead of a date. I have removed the calculation to get the 14 days greater than the bill date because this is where I think my mistake is.

Can someone with javascript experience take a look at this code & see what I need to do to get the inventory date field to be 14 days greater than the bill date?
<html>
<head>
<title>My Title</title>

<script type="text/javascript">
var valid;

function dcheck(form) {
var d = form.dueDate.value;
var b = form.billDate.value;
var i = form.intvDate.value;
var due  = new Date(d);
var bill = new Date(b);
var intv = new Date(i);

if (d && b && i) {
alert ("You may not fill in both boxes!");
return false;
}
if (isNaN(due)) {
if (isNaN(bill)) {
alert("Neither Due Date nor Billing Date given!");
return false;
}
}

if (isNaN(bill)) {
var dsplit = [];
dsplit = d.split("/");
var test = checkValidDate (dsplit[2], dsplit[0], dsplit[1]);
if (!test) {return false}
}

if (isNaN(due)) {
var bsplit = [];
bsplit = b.split("/");
var test = checkValidDate (bsplit[2], bsplit[0], bsplit[1]);
if (!test) {return false}
}

if (isNaN(due)) {
due = new Date(bill.getFullYear(),bill.getMonth(),bill.getDate()+7);
} 
else if (isNaN(bill)) {
bill = new Date(due.getFullYear(),due.getMonth(),due.getDate()-7);
}
else if (isNaN(bill)) {
intv = new Date(due.getFullYear(),due.getMonth(),due.getDate()-7);
}
form.dueDate.value = due.getMonth()+1 + "/" + due.getDate() + "/" + due.getFullYear();
form.billDate.value = bill.getMonth()+1 + "/" + bill.getDate() + "/" + bill.getFullYear();
form.intvDate.value = intv.getMonth()+1 + "/" + intv.getDate() + "/" + intv.getFullYear();
return true;
}

function checkValidDate(yr,mmx,dd) {

if (yr <2000 || yr >2020) {  
alert ("Invalid year!");
valid = false;
return valid;
}

mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
valid = false; 
}
else {
alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
valid = true;
}
return valid;
}


</script>

<style type="text/css">
body {background-color: #ffffff; margin-top: 60px;}	
</style>

</head>
<body>

<form action="" method="get" onsubmit="return dcheck(this);">
<label>Bill Date MM/DD/YYYY: <input type="text" size = "10" name="billDate"></label>
<br>
<label>Due Date MM/DD/YYYY:&nbsp &nbsp <input type="text" size = "10" name="dueDate"></label>
<br>
<label>Inventory Date MM/DD/YYYY:&nbsp &nbsp <input type="text" size = "10" name="intvDate"></label>
<br>
<input type="button" value="Calculate" onclick="return dcheck(this.form);">	
<input type="button" value="Reset" onclick="this.form.reset()">				
</form>

</body>
</html>

Open in new window

0
Comment
Question by:wantabe2
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35017852
You had a typo.

Also replaced elsif with if in two places.

<html>
<head>
<title>My Title</title>

<script type="text/javascript">
var valid;

function dcheck(form) {
var d = form.dueDate.value;
var b = form.billDate.value;
var i = form.intvDate.value;
var due  = new Date(d);
var bill = new Date(b);
var intv = new Date(i);

if (d && b && i) {
alert ("You may not fill in both boxes!");
return false;
}
if (isNaN(due)) {
if (isNaN(bill)) {
alert("Neither Due Date nor Billing Date given!");
return false;
}
}

if (isNaN(bill)) {
var dsplit = [];
dsplit = d.split("/");
var test = checkValidDate (dsplit[2], dsplit[0], dsplit[1]);
if (!test) {return false}
}

if (isNaN(due)) {
var bsplit = [];
bsplit = b.split("/");
var test = checkValidDate (bsplit[2], bsplit[0], bsplit[1]);
if (!test) {return false}
}

if (isNaN(due)) {
due = new Date(bill.getFullYear(),bill.getMonth(),bill.getDate()+7);
} 
if (isNaN(bill)) {
bill = new Date(due.getFullYear(),due.getMonth(),due.getDate()-7);
}
if (isNaN(intv)) {
intv = new Date(due.getFullYear(),due.getMonth(),due.getDate()-7);
}
form.dueDate.value = (due.getMonth()+1) + "/" + due.getDate() + "/" + due.getFullYear();
form.billDate.value = (bill.getMonth()+1) + "/" + bill.getDate() + "/" + bill.getFullYear();
form.intvDate.value = (intv.getMonth()+1) + "/" + intv.getDate() + "/" + intv.getFullYear();
return true;
}

function checkValidDate(yr,mmx,dd) {

if (yr <2000 || yr >2020) {  
alert ("Invalid year!");
valid = false;
return valid;
}

mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
valid = false; 
}
else {
alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
valid = true;
}
return valid;
}


</script>

<style type="text/css">
body {background-color: #ffffff; margin-top: 60px;}	
</style>

</head>
<body>

<form action="" method="get" onsubmit="return dcheck(this);">
<label>Bill Date MM/DD/YYYY: <input type="text" size = "10" name="billDate"></label>
<br>
<label>Due Date MM/DD/YYYY:&nbsp &nbsp <input type="text" size = "10" name="dueDate"></label>
<br>
<label>Inventory Date MM/DD/YYYY:&nbsp &nbsp <input type="text" size = "10" name="intvDate"></label>
<br>
<input type="button" value="Calculate" onclick="return dcheck(this.form);">	
<input type="button" value="Reset" onclick="this.form.reset()">				
</form>

</body>
</html>

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
ID: 35017955
If I type in 05/10/2005 I get 05/17/2005 in the due date which is correct but I am getting 05/10/2005 in the inventory date field now...how can I get the date in the inventory date field to be 14 days greater than the date in the bill date field?
0
 
LVL 15

Author Closing Comment

by:wantabe2
ID: 35017989
I got it thanks
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35018003
<html>
<head>
<title>My Title</title>

<script type="text/javascript">
var valid;

function dcheck(form) {
var d = form.dueDate.value;
var b = form.billDate.value;
var i = form.intvDate.value;
var due  = new Date(d);
var bill = new Date(b);
var intv = new Date(i);

if (d && b && i) {
alert ("You may not fill in both boxes!");
return false;
}
if (isNaN(due)) {
if (isNaN(bill)) {
alert("Neither Due Date nor Billing Date given!");
return false;
}
}

if (isNaN(bill)) {
var dsplit = [];
dsplit = d.split("/");
var test = checkValidDate (dsplit[2], dsplit[0], dsplit[1]);
if (!test) {return false}
}

if (isNaN(due)) {
var bsplit = [];
bsplit = b.split("/");
var test = checkValidDate (bsplit[2], bsplit[0], bsplit[1]);
if (!test) {return false}
}

if (isNaN(due)) {
due = new Date(bill.getFullYear(),bill.getMonth(),bill.getDate()+7);
} 
if (isNaN(bill)) {
bill = new Date(due.getFullYear(),due.getMonth(),due.getDate()-7);
}
if (isNaN(intv)) {
intv = new Date(bill.getFullYear(),bill.getMonth(),bill.getDate()+14);
}
form.dueDate.value = (due.getMonth()+1) + "/" + due.getDate() + "/" + due.getFullYear();
form.billDate.value = (bill.getMonth()+1) + "/" + bill.getDate() + "/" + bill.getFullYear();
form.intvDate.value = (intv.getMonth()+1) + "/" + intv.getDate() + "/" + intv.getFullYear();
return true;
}

function checkValidDate(yr,mmx,dd) {

if (yr <2000 || yr >2020) {  
alert ("Invalid year!");
valid = false;
return valid;
}

mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
valid = false; 
}
else {
alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
valid = true;
}
return valid;
}


</script>

<style type="text/css">
body {background-color: #ffffff; margin-top: 60px;}	
</style>

</head>
<body>

<form action="" method="get" onsubmit="return dcheck(this);">
<label>Bill Date MM/DD/YYYY: <input type="text" size = "10" name="billDate"></label>
<br>
<label>Due Date MM/DD/YYYY:&nbsp &nbsp <input type="text" size = "10" name="dueDate"></label>
<br>
<label>Inventory Date MM/DD/YYYY:&nbsp &nbsp <input type="text" size = "10" name="intvDate"></label>
<br>
<input type="button" value="Calculate" onclick="return dcheck(this.form);">	
<input type="button" value="Reset" onclick="this.form.reset()">				
</form>

</body>
</html>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
split string containing \r\n in Java 46 38
web services creation SOAP vs REST 5 21
fibonacci ten numbers 4 30
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now