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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datecheck script

Status
Not open for further replies.

jwc194

Programmer
Aug 1, 2002
2
0
0
US
I'm pretty new to javascript programming so I'm wondering if someone could help me out. I have a form where the user enters 2 dates (start_date and end_date). I would like to use a script to make sure that when the user hits submit, it will make sure that the end_date does not come before the start_date. As I said before, I'm new to javascript so any help would be appreciated. Thanks.
 
FYI Heres the script the way I have it now (which doesn't work). This is just some stuff I pieced together that I found on the internet. Any help would be appreciated.

<script language=&quot;javascript&quot;>
function compareDates(document.member.MbrCat_EffectiveDate,M/d/yyyy,document.member.MbrCat_TerminationDate,M/d/yyyy) {
var d1=getDateFromFormat(document.member.MbrCat_EffectiveDate,M/d/yyyy);
var d2=getDateFromFormat(document.member.MbrCat_TerminationDate,M/d/yyyy);
if (d1==0 || d2==0) {
alert(&quot;The dates can not be the same.&quot;);
return false;
}
else if (d1 > d2) {
alert(&quot;The termination date can not come before the effective date.&quot;)
return false;
}
else {
return true;
}
</script>

Here is the part of the form that im getting the information from:

<FORM name=&quot;member&quot; onsubmit=&quot;return compareDates(document.member.MbrCat_EffectiveDate,M/d/yyyy,document.member.MbrCat_TerminationDate,M/d/yyyy);&quot; <!--ACTION=&quot;member_2edit.cfm?id=#MbrCat_rec_nbr#&quot;--> METHOD=&quot;Post&quot;>
.
.
.
<input type=&quot;VARCHAR&quot; name=&quot;MbrCat_EffectiveDate&quot; size=&quot;11&quot; maxlength=&quot;11&quot; width=&quot;50%&quot;><font size=&quot;2&quot;> (mm/dd/yyyy)</font>
.
.
.
<input type=&quot;VARCHAR&quot; name=&quot;MbrCat_TerminationDate&quot; size=&quot;11&quot; maxlength=&quot;11&quot; width=&quot;50%&quot;><font size=&quot;2&quot;> (mm/dd/yyyy)</font>
.
.
.
Thanks.
 
Here is a date checker function.

If I have time I'll try and add a comparison function to it as well.

It's probably not the most elegant solution, but hey, it works (at least on my browser and platform of choice). If you don't decide to use it hopefully it will at lest give you some ideas.



/*checkDates written by Jaxon Johns 08/02/2002
Use it all you want , but it would be cool of you to give me credit. : )

This script checks two dates to be in the proper form of mm/dd/yyyy.

the two arguments are the two date strings you want checked.

It can easily handle more than two dates by adding more arguments and increasing the first for loop accordingly.

I don't have the time or resources to check it on every platform/browser combo, so hopefully it works on most.

N-Joi
*/


function checkDates (e,t) {

a = checkDates.arguments;
userError = 0;

/*checking to see if all the slashes are in the right place
this check is very strict in that you must enter the month and date as two digits each*/
outsideTheLoop://this is a lable for the break function in the nested for loop... see below
for (i = 0; i < 2 ; i++) {
CDarray=a.split(&quot;/&quot;);
if (a.length==10&&a.indexOf(&quot;/&quot;)==2&&a.lastIndexOf(&quot;/&quot;)==5){
} else {
alert(&quot;The date(s) must be in the form mm/dd/yyyy&quot;);
userError=1;
break;
}
//now we are checking to see if all the input between the '/'s were numbers
for (j=0; j<3 ; j++) {
if (isNaN(CDarray[j])){
alert(&quot;only numeric values and the '/' character are allowed in the date field.&quot;);
userError=1;
break outsideTheLoop;
}
}
}


if (userError==!1) {
alert(&quot;Thank you for submiting the form.&quot;);
//put code for sending the form here
}

}


***************************************************
Here is an example of the form I used for testing it.

<form name=&quot;member&quot; onSubmit=&quot;checkDates(document.member.effectiveDate.value, document.member.terminationDate.value);&quot;>
effective date <input name=&quot;effectiveDate&quot; type=&quot;text&quot; size=&quot;11&quot; maxlength=&quot;11&quot;>
termination date <input name=&quot;terminationDate&quot; type=&quot;text&quot; size=&quot;11&quot; maxlength=&quot;11&quot;>
<input name=&quot;enter&quot; type=&quot;submit&quot;>
</form>




Hope this helps,

jaxon

 
Ooooooor.....

you could do this...

thread216-327821

In your case this...
Code:
function checkDates(date1,date2)
{
  inpDate = checkDates.arguments;
  for (i=0;i<inpDate.length;i++)
    {
      var filter=/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
      if (! filter.test(inpDate[i]))
        {  
          alert(&quot;Please enter Date in DD/MM/YYYY Format !&quot;);
          return false; 
        }
    }
 }
...to do exactly what I did in the post above. You can use the same form markup to test it.

Oh well, it was a pretty good script for my second attempt at javaScript. [purple]

Note: I couldn't get this to work in IE5+ on the Mac.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top