Solved

JavaScript Code Help

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 40
How to get all the API from website? 11 82
Regular Expression Calculator Tester 2 71
arguments to jar 5 26
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…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

815 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

9 Experts available now in Live!

Get 1:1 Help Now