[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Compare two dates but does not alert second message.

Iam using onMouseOver event. When a user moves the cursor over the submit button, if any date is empty, it alerts the first message-- Please select From & Thru dates to proceed.". But when i put from date to be greater than thru date, it does not print second alert message. Below is partial code. Any suggestions as to why it is not alerting second message.

function doDateCheck(from,thru) {
       if ((document.form1.from_date.value == "") || (document.form1.thru_date.value == "")){
      alert("Please select From & Thru dates to proceed."); // 1st alert message
      checkdateValidity(this.form1.from_date, this.form1.thru_date);
      return false;
      }
      
}

function checkdateValidity(from, thru) {
      if (Date.parse(from) >= Date.parse(thru)){
                  alert("From Date must be earlier than To (Thru) date"); // 2nd alert message
                  return false;
      }

}


function checkvalidation() {
      if (document.form1.CUSIP.selectedIndex == 0)      {
            alert("Please select a CUSIP");
            doDateCheck(this.form1.from_date, this.form1.thru_date);
            return false;
      }
      return true;
}
--------------------Submit button----
<input name="submit" type="submit" value="Search" onMouseOver="doDateCheck(document.form1.from_date.value,document.form1.thru_date.value)" onclick= "return checkvalidation();">
0
fzahir
Asked:
fzahir
1 Solution
 
BogoJokerCommented:
Hi fzahir,

Try adding the .value?
     checkdateValidity(this.form1.from_date.value, this.form1.thru_date.value);


Joe P
0
 
fzahirAuthor Commented:
I added the value, still it does not work.

faiz
0
 
dakydCommented:
Assuming you're passing from_date.value and thru_date.value, give this a shot:

function checkdateValidity(from, thru) {
  var fromDate = new Date(from);
  var thruDate = new Date(thru);
  if (fromDate.getTime() >= thruDate.getTime()){
    alert("From Date must be earlier than To (Thru) date"); // 2nd alert message
    return false;
  }
  return true;
}

Basically, I just changed how you parse the string to get a Date object.  Hope it helps.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Peter KwanCommented:
Please try to change the this.form1.from_date (same as thru_date) to document.form1.from_date.value.
0
 
fzahirAuthor Commented:
None of the solutions work. I modified the code as per suggestions. I put some alerts for fromDate and thruDate. It does not print these alerts. Also when i remvoed one date, it prints the first alert message, and then prints fromDate and thruDate but does not print these values. Any suggestions.

function checkdateValidity(from, thru) {

      var fromDate = new Date(from);
      var thruDate = new Date(thru);
      alert("fromDate");
      alert("thruDate");
      if (fromDate.getTime() >= thruDate.getTime()){
                  alert("From Date must be earlier than To (Thru) date");
                  return false;
      }
      return true;
}
0
 
dakydCommented:
*sigh* Sometimes, I'm just stupid.  I didn't notice at first, but you're calling your functions in funny places - for example, you're checking if from is before thru inside of the if clause of doDateCheck.  That guarantees that you can only check the two dates if one of them is blank (probably not what you intended).  The same is also true of calling doDateCheck only when a CUSIP wasn't selected; again, probably not what you intended.

The following should be a little more sane - notice where the functions are being called now.  Also, the reason you weren't getting values in your alerts was that you passed strings (i.e. "fromDate" and "thruDate") - if you want the values, you should've done alert(fromDate.toString()).

Anyhow, let's see if this is any better.

function checkvalidation() {
     if (document.form1.CUSIP.selectedIndex == 0)     {
          alert("Please select a CUSIP");
          return false;
     }
     return doDateCheck(this.form1.from_date, this.form1.thru_date);;
}

function doDateCheck(from,thru) {
  if ((document.form1.from_date.value == "") || (document.form1.thru_date.value == ""))
  {
    alert("Please select From & Thru dates to proceed."); // 1st alert message
     return false;
  }
  return checkdateValidity(this.form1.from_date.value, this.form1.thru_date.value);
}

function checkdateValidity(from, thru) {
  var fromDate = new Date(from);
  var thruDate = new Date(thru);
  if (fromDate.getTime() >= thruDate.getTime()){
    alert("From Date must be earlier than To (Thru) date"); // 2nd alert message
    return false;
  }
  return true;
}
0
 
fzahirAuthor Commented:
This solution works like a charm. Even i realised that i was calling the function at the wrong place. Thanks for quick solution.

faiz
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now