Solved

JavaScript Code Help

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bot application - advice 3 59
Developing a front end to SPLUNK 1 56
Java array 21 81
Strange router problem - can't access hotmail.com 14 39
A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

756 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