date error alert

i want my date to indicate an error msg to indicaite if an end date is  in the past  this is the java script i have and it dont seem to work

var

ErrorCount = 0;

var

dteStartDate = GetField("dteStartDate", "");

var

dteEndDate = GetField("dteEndDate", "");

while

(ErrorCount == 0)

{

if

(dteStartDate < dteEndDate)

{

  

//alerting the user to select a valid time

   alert("Please select a valid time that the Audit should start!");

   ErrorCount = 1;

 

break;

}

ErrorCount = 1

}

Tagged:

Comments

  • GetField function returns string, so you don't compare dates, but strings.
    If you want to compare dates, you need to convert them to JavaScript dates.
    http://www.w3schools.com/js/js_obj_date.asp
    Take a look at this JavaScript&colon;

    var tmpStart = strStartDate.split('.');
    var tmpEnd   = strEndDate.split('.');
    
    var dtStartDate = new Date(parseInt(tmpStart[2], 10), parseInt(tmpStart[1], 10), parseInt(tmpStart[0], 10));
    var dtEndDate = new Date(parseInt(tmpEnd[2], 10), parseInt(tmpEnd[1], 10), parseInt(tmpEnd[0], 10));
    
    if(dtStartDate < dtEndDate)
    {
      ...
    }
    

     

    This is not robust code which presume, that in strStartDate is date in format d.m.Y (day, month, year, separated by periods). Date is splitted to three integers, which are passed to Date constructor. I guess that you use other date format in your country, so you will need to modify it slightly. Also, end users could use other date format, so client side check might be tricky.

  • Is there any reason not to merely set the limit of the date field to today?