Skip to main content
Known Participant
April 27, 2009
Question

Javascript hel - how to limit date selection ?

  • April 27, 2009
  • 1 reply
  • 1353 views

I  use the following javascript to validate a start and end date, and radio buttons on my form. What I need to do is limit the date selection to 30 days only. Anything more should display an error. So if I enter starting date of 1/1/2009 and ending date of 2/28/2009, then I should get a popup error. How can I do this with my exisintg code :

<Script language="JavaScript">

  function validateForm()
{

if(document.indexForm.startDate.value == "" && document.indexForm.endDate.value == "")
     {
    alert('Start Date and End Date cannot be blank.');
    indexForm.startDate.focus();
    return(false);
    }
      
if(document.indexForm.startDate.value > document.indexForm.endDate.value)
     {
    alert('End Date cannot be less than Start Date.');
    indexForm.endDate.focus();
    return(false);
    }

if(document.indexForm.startDate.value !== "" && document.indexForm.endDate.value == "")
     {
    alert('End Date cannot be blank.');
    indexForm.endDate.focus();
    return(false);
    }
   
if(document.indexForm.startDate.value == "" && document.indexForm.endDate.value !== "")
     {
    alert('Start Date cannot be blank.');
    indexForm.startDate.focus();
    return(false);
    }
myOption = -1;
    for (i=indexForm.region_code.length-1; i > -1; i--) {
    if (indexForm.region_code.checked) {
    myOption = i; i = -1;
    }
    }
    if (myOption == -1) {
    alert("Please select a region.");
     return(false);
    }

myOption = -1;
    for (i=indexForm.reportName.length-1; i > -1; i--) {
   if (indexForm.reportName.checked) {
   myOption = i; i = -1;
    }
    }
    if (myOption == -1) {
   alert("Please select a report.");
     return(false);
   }

                   
return(true);
}


</Script>

This topic has been closed for replies.

1 reply

Inspiring
April 28, 2009

Step 1.  Get rid of anything that can be done with a cfinput tag and the built in js that comes with it.

Step 2 - js is reading strings, not dates from your text boxes.  Convert these to dates before you start comparing the values to each other.