0

I want to perform a validation for today's date and user entered date in my custom list form's multiple datetime fields

  • ReqstRecvdDate Field
  • DueByDate field
  • ReqstRecvdDate can not be greater than today's date.It can be equal to today's date or less than today's date.
  • if RecvdDate>=Today , I need to prevent user from saving the form- presaveaction call.

    Also DueByDate field, which has to be greater than ReqstRecvdDate Field. I added the below code, but the validation is not working when

    a) I selected 31-Oct-2015 as ReqstRecvdDate & I selected DueByDate field as 01-Dec-2015. As per the rule it's set to true and should allow me to save the form, but this didn't happen.

           <script language="javascript" 
           src="/project/EASAssetsLib/jquery-1.6.0.min.js" 
           type="text/javascript"></script><script language="javascript" 
             src="/project/EASAssetsLib/jquery.SPServices-
            2014.01.min.js" type="text/javascript"></script><script 
         language="javascript" 
            src="/project/EASAssetsLib/prototype.js" 
           type="text/javascript"></script><script language="javascript" 
           src="/project/EASAssetsLib/SPUtility.js" 
           type="text/javascript"></script><script language="javascript" 
            type="text/javascript">
    
         function PreSaveAction()
          {
           var reqstrecvddatethrusputility = SPUtility.GetSPField('Request 
           Received Date').GetValue(); 
          var reqsteDateStr=reqstrecvddatethrusputility.toString();
          var mon1  = reqsteDateStr.substring(0,2); 
           var dt1 = reqsteDateStr.substring(3,5); 
           var yr1  = reqsteDateStr.substring(6,10);  
           temp1 = mon1 + "/" + dt1 + "/" + yr1;
           var dtReqstRecvdDate = new Date(temp1);
           var reqstRecvdDateInUTC = dtReqstRecvdDate.toUTCString();
            var currentTime = new Date();
           var CurDateInUTC = currentTime.toUTCString();
          dt3 = new Date(reqstRecvdDateInUTC);
          dt4 = new Date(CurDateInUTC);
       if(dt3 > dt4 )
         {
           alert('Received Date cannot be greater than Todays  
                   Date!');
           return false;
         } 
    

    Due to some reason,month range is not getting recognized properly.

    What I need to change in my code to do the right validation?

    0

    1 Answer 1

    2

    Doing String operations on Dates is never a good habit.

    You are adding so many libraries allready.
    Adding momentJS to make date calculations a breeze won't be an issue..

    Your Month problem is probably because in JavaScript january === 0 , thus december === 11

    6
    • I downloaded the moment.js into my assets lib.How to use it with dates which is coming from SharePoint datefield?
      – samolpp2
      Commented Nov 2, 2015 at 10:12
    • I got the date format value like this: var reqstrecvddate2 = SPUtility.GetSPField('Request Received Date').GetValue(); var duebydate2 = SPUtility.GetSPField('Due By').GetValue(); var reqstrecvddate2moment = moment(reqstrecvddate2).format("MM/DD/YYYY"); alert(reqstrecvddate2moment + ' ...from moment' ); But how to compare the reqstrecvddate2 against duebydate2moment ?
      – samolpp2
      Commented Nov 2, 2015 at 10:19
    • You are taking too many steps at once. First play with/learn JavaScript date calculations, then make it work in SharePoint. Your problem is not SharePoint. Your problem is you do not understand working with JavaScript dates. Commented Nov 2, 2015 at 10:25
    • Agreed!.Let me work on that and revert.
      – samolpp2
      Commented Nov 2, 2015 at 10:43
    • 1
      sitepoint.com/7-code-playgrounds Commented Nov 2, 2015 at 11:05

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.