Jump to content

Date Check To See If Date A Is Prior To Date B By X Days


Recommended Posts

Hi I am looking to see if there is a script that would do a date check between two to dates entered into a submission form.

 

I need to have a check to see if date A is a minimum of 2 days prior to date B. If it's not the form can not be submitted and an alert message appears.

 

I attempted to modify a couple other scripts I found but was unsuccessful at getting them to work.

 

Any assistance would be greatly appreciated!

Link to comment
Share on other sites

Hello Liz,

 

If I understand correctly what is the goal, you can try this code:

<SCRIPT LANGUAGE="JavaScript">
function checkStart()
{
var v_aDate = document.getElementById("InsertRecordA").value;
var v_bDate = document.getElementById("InsertRecordB").value;
var aDate = new Date(v_aDate);
var bDate = new Date(v_bDate);
bDate.setDate(bDate.getDate()-3);
if (aDate>bDate)
   {
      alert("date A should be a minimum of 2 days prior to date B");
      return false;
    }
}   
document.getElementById("caspioform").onsubmit=checkStart;
</SCRIPT>
Link to comment
Share on other sites

Hi Jan,

 

Thank you so much this works perfectly stand alone but it appears to be stopping my other date check script. I am pasting them both below. Perhaps you could offer some advice on getting both of these to work? They are both in the footer of my form:

 

The one from above:

 

<SCRIPT LANGUAGE="JavaScript">
function checkTenderDate()
{
var v_aDate = document.getElementById("InsertRecordTenderTurnOffDate_Web").value;
var v_bDate = document.getElementById("InsertRecordInStoreSFA_End").value;
var aDate = new Date(v_aDate);
var bDate = new Date(v_bDate);
bDate.setDate(bDate.getDate()-3);
if (aDate>bDate)
   {
      alert("Tender Turn Off date should be a minimum of 2 days prior to SFA End date");
      return false;
    }
}   
document.getElementById("caspioform").onsubmit=checkTenderDate;
</SCRIPT>
 
 
 
The one already in the form:
 
<SCRIPT LANGUAGE="JavaScript">
 function checkStart()
{
   var v_sDate = document.getElementById("InsertRecordInStoreSFA_Start").value;
   var v_eDate = document.getElementById("InsertRecordInStoreSFA_End").value;
   var sDate = new Date(v_sDate);
   var eDate = new Date(v_eDate);
   var nDate = new Date();
   nDate = Date.now();
   var setToday = sDate.getDate() + 1;
   sDate.setDate(setToday);
   setToday = eDate.getDate() + 1;
   eDate.setDate(setToday);
   if (sDate<nDate)
      {
         alert("SFA start date must be greater than today");
         return false;
      }
      else
        {
            if (eDate<sDate)
              {
                  alert("SFA end date must be greater than SFA start date");
                  return false;
              }
        }
}   
document.getElementById("caspioform").onsubmit=checkStart;
</SCRIPT>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...