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!

form validation - date comparison 1

Status
Not open for further replies.

jesse1

Programmer
Aug 29, 2008
30
US
I am new with javascript. I thought the folowing would work but it does not. The problem seems to be with the date validation section. Any help is appreciated.
Code:
<script language="JavaScript" type="text/JavaScript">
function validate() {


if (document.mainform.confname.value=="" ) 
{
alert("Please enter your Conference Name");
event.returnValue=false;
}
if (Date.parse(document.mainform.opendate.value) <= Date.parse(document.mainform.closedate.value)) {
alert("Registration End Date must occur after the Registraton Start date.");
event.returnValue=false;
)
}
</SCRIPT>
Code:
<form action="setup.cfm" method="post" onSubmit="validate();" name= "mainform" id= "mainform">
 
To get yourself out of the trouble of hooking to ie propertary, use cross-browser structure. Also slight elaboration on the date-time parsing results is needed.
[tt]
function validate() {

if (document.mainform.confname.value=="" )
{
alert("Please enter your Conference Name");
[red]//[/red]event.returnValue=false;
[blue]return false;[/blue]
}
var dt1=Date.parse(document.mainform.opendate.value);
var dt2=Date.parse(document.mainform.closedate.value);
if (isNaN(dt1) or isNaN(dt2) or (dt1 <= dt2)) {
alert("Date entries are erroneous in format or Registration End Date must occur after the Registraton Start date.");
[red]//[/red]event.returnValue=false;
[blue]return false;[/blue]
)
[blue]return true;[/blue]

}
[/tt]
and modify the form tag.
[tt]
<form action="setup.cfm" method="post" onSubmit="[blue]return[/blue] validate();" name= "mainform" id= "mainform">[/tt]
 
I get the following error.

[highlight]Expected ')'[/highlight]

I tried change the ) after the last return false to } but I still got the same error.

Thanks
 
I don't know... I can't see the brace "}" vs bracket ")" clearly from your script which may propagate to my version. This kind of trivial thing you've to take care of it yourself easily.
[tt]
function validate() {

if (document.mainform.confname.value=="" )
{
alert("Please enter your Conference Name");
//event.returnValue=false;
return false;
}
var dt1=Date.parse(document.mainform.opendate.value);
var dt2=Date.parse(document.mainform.closedate.value);
if (isNaN(dt1) or isNaN(dt2) or (dt1 <= dt2)) {
alert("Date entries are erroneous in format or Registration End Date must occur after the Registraton Start date.");
//event.returnValue=false;
return false;
[highlight]}[/highlight]
return true;

}
[/tt]
Other than that, I don't see anything wrong.
 
I am sorry, I was not clear.
It did not work either way. After it did not your way I tried to edit it. By edit, I mean I just took a guess at what the problem might be.
The error I referred to was generated by Internet Explorer.

Thanks again.
 
Then it has nothing to do with what shown.
 
Thanks. I just modified it and this one works.
Code:
function validate() {


if (document.mainform.confname.value=="" ) 
{
alert("Please enter the Conference Name");
event.returnValue=false;
}
if (Date.parse(document.mainform.opendate.value) > Date.parse(document.mainform.closedate.value)) 
{
alert("Registration End Date must occur after the Registraton Start date.");
event.returnValue=false;
}
}

Code:
<form action="p_consetup2.cfm" method="post" onSubmit="validate();"  name= "mainform" id= "mainform">
 
>...and this one works.
[tt]...and this one works in ie.[/tt]
 
Good point it did not work in Firefox. This one works in both?

Code:
<script language="JavaScript" type="text/JavaScript">
function validate() {


if (document.mainform.confname.value=="" ) 
{
alert("Please enter the Conference Name");
return false;
}
if (Date.parse(document.mainform.datename.value) <= Date.parse(document.mainform.opendate.value)) 
{
alert("Registration START Date must occur before the Conference Date.");
return false;
}


if (Date.parse(document.mainform.opendate.value) > Date.parse(document.mainform.closedate.value)) 
{
alert("Registration END Date must occur after the Registraton START Date.");
return false;
}

}
</script>
Code:
                  <form action="psetup.cfm" method="post" onSubmit="return validate();"  name= "mainform" id= "mainform">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top