Solved

JavaScript Code Help

Posted on 2011-03-02
4
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java class and jar 3 58
Survey branching tutorial 11 73
Java import explained 4 44
Cannot locate cell 15 37
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 article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

738 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