Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare Dates 2

Status
Not open for further replies.

rmz8

Programmer
Aug 24, 2000
210
US
Is there any way to compare to dates in seperate form fields to make sure that one is later than the other?

Ryan ;-]
 
darn, i wrote a script to do that a while ago, but its at work... if you can wait till monday, i'll post it.

adam@aauser.com
 
That would be great. Thanks so much.

Ryan ;-]
 
<script>
function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f > n)
{
alert(&quot;good&quot;);
}
else
{
alert(&quot;bad&quot;);;
}

}

todayDate(&quot;01/02/2000&quot;, &quot;01/01/2000&quot;)
</script> adam@aauser.com
 
How do I implement this with form values?

Ryan ;-]
 
todayDate(document.formname.date1.value,document.formname.date2.value)

should work if your form is named &quot;formname&quot; and your date elements are named &quot;date1&quot; and &quot;date2&quot; jared@aauser.com
 
But how do i call to it? In other words, here's what I put in <HEAD>:

[tt]function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f > n)
{

}
else
{
alert(&quot;Please enter an assigned date that is before the due date.&quot;);;
}

}

todayDate(document.form.AssignedDate.value,document.form.DueDate.value)[/tt]

So how can I make this script run when the form is submitted? Like onSubmit i]WHAT[/i].

Ryan ;-]
 
only put the function definition in <head>, between script tags of course

for your form, do: <form ... onsubmit=&quot;return todayDate(document.form.AssignedDate.value, document.from.DueDate.value)&quot;>

if you are going to do an onsubmit, you should put &quot;return false;&quot; in the else branch of the function's if statement, after the alert ray
rheindl@bju.edu

 
I did all of that. And it works. But regardless of whether the alert is encountered, the form continued to submit.

Ryan ;-]
 
add the line:

return false;

after the alert and use:

onsubmit=&quot;return (document.form.AssignedDate.value, document.from.DueDate.value)&quot; jared@aauser.com
 
Now no alert comes up at all.

Ryan ;-]
 
function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f > n)
{
return true;
}
else
{
return false;
}

}


then call it onsubmit. adam@aauser.com
 
Here is my script (I wanted[/] to change f < n):

function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f < n)
{
return true;
}
else
{

return false;
}

}


Here is my form tag:

<form action=&quot;add_update.cfm&quot; method=&quot;post&quot; name=&quot;form&quot; onSubmit=&quot;
todaydate(document.form.AssignedDate.value, document.form.DueDate.value)&quot;>

Ryan ;-]
 
the following accomplishes what you want:

<script>
function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f <= n)
{
return true

}
else
{

alert(&quot;Please enter an assigned date that is before the due date.&quot;);
return false;
}

}
</script>
<form name=&quot;formname&quot; onsubmit=&quot;return todayDate(document.formname.AssignedDate.value,document.formname.DueDate.value)&quot;>
<input name=&quot;AssignedDate&quot;>
<input name=&quot;DueDate&quot;>
<input type=&quot;submit&quot;>
</form> jared@aauser.com
 
onSubmit=&quot;return todaydate(document.form.AssignedDate.value, document.form.DueDate.value)&quot;> adam@aauser.com
 
It doesn't work when I set it up like you said, Jared.

Ryan ;-]
 
Oh, I found the error. Single quotes must surround each element in the onSubmit function.

Ryan ;-]
 
The code doesn't work. After much toying I can't seem to find what's wrong with it (it comes up with the alert regardless of what the dates are):

function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f > n)
{
return true;

}
else
{

alert(&quot;Please enter an assigned date that is before the due date.&quot;);
return false;
}

}

And in the submit button I have the following:

<INPUT TYPE=&quot;submit&quot; NAME=&quot;Submit&quot; VALUE=&quot;Submit&quot; onclick=&quot;return todayDate('document.form.AssignedDate.value','document.form.DueDate.value')&quot;>

I have the file uploaded on a live server. Go to:

UserName: jbernhardt
PW: test

Then click on 'Add a new assignment' to view the form in question.



Ryan ;-]
 
you keep changing it... look at this :eek:)

<script>
function todayDate(date, old)
{
var f = new Date(date);
var n = new Date(old);
if (f <= n)
{
return true

}
else
{

alert(&quot;Please enter an assigned date that is before the due date.&quot;);
return false;
}

}
</script> jared@aauser.com
 
That says if f is less than or equal to. I want it simply great than! :)

Ryan ;-]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top