Solved

JavaScript Code Help

Posted on 2011-03-02
4
327 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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

16 Experts available now in Live!

Get 1:1 Help Now