Date fields

Forums General discussion Date fields

Viewing 5 reply threads
  • Author
    Posts
    • #31467
      First Eagle
      Participant

      Hello Alexander,

      please i’m creating a form with several date fields and want to setup a rules to control the first date and last date :

      – The first date and last date should be the days in the current month or in the preview month (Means for the user filling the form in September, he should be able to select a date only for August and September).

      – The last date should not be older then the first date.
      – if we’ve several periode (first date1 – Last date 1, first date2 – Last date 2, …) , the second period (first date2 – Last date 2) cannot be older then the first period (first date1 – Last date 1)

      please help.

      Best regards

    • #31476
      Alexander Bautz
      Keymaster

      I don’t have any solution for limiting the selectable date in the date picker, but it is possible to use js to check the date and prevent save if the date is out of range.

      I don’t understand what you mean by:

      if we’ve several periode (first date1 – Last date 1, first date2 – Last date 2, …) , the second period (first date2 – Last date 2) cannot be older then the first period (first date1 – Last date 1)

      Is this date ranges within the same form, or another list item submitted earlier for the same user?

      Alexander

    • #31478
      First Eagle
      Participant

      Hello Alexander,

      thanks for your feedback,

      I don’t have any solution for limiting the selectable date in the date picker, but it is possible to use js to check the date and prevent save if the date is out of range.

      R/ Do you have a js code for that ? please help me.

      Is this date ranges within the same form, or another list item submitted earlier for the same user?

      R/ The ranges are in the same form.

      Also, how to prevent the cretion of two items for the same month ?

      Best regards

    • #31497
      Alexander Bautz
      Keymaster

      Her is a code example for checking that start date is larger then last month first day and that end date is less than current month last day.

      function getFirstDayLastMonth(){
          var a = new Date();
          a.setHours(12,0,0,0);
          var b = new Date(a.setMonth(a.getMonth()-1));
          var c = b.setDate(1);
          return new Date(c);
      }
      
      function getLastDayThisMonth(){
          var a = new Date();
          a.setHours(12,0,0,0);
          var b = new Date(a.setMonth(a.getMonth()+1));
          var c = b.setDate(0);
          return new Date(c);
      }
      
      function checkDateSpan(startDateField, endDateField){
          var firstSelectableDate = getFirstDayLastMonth();
          var lastSelectableDate = getLastDayThisMonth();
          var startDate = spjs.utility.getDateFieldAsDateObject(startDateField);
          startDate.setHours(12,0,0,0);
          var endDate = spjs.utility.getDateFieldAsDateObject(endDateField);
          endDate.setHours(12,0,0,0);
          var pass = true;
          // Check start date
          if(startDate < firstSelectableDate || startDate > lastSelectableDate){
              pass = false;
              spjs.dffs.alert({
                  "title": "Start date out of selectable date range",
                  "msg": "You can select dates between " + firstSelectableDate.toDateString() + " and " + lastSelectableDate.toDateString()
              });
          }
          // Check end date
          if(endDate > firstSelectableDate || endDate < firstSelectableDate){
              pass = false;
              spjs.dffs.alert({
                  "title": "End date out of selectable date range",
                  "msg": "You can select dates between " + firstSelectableDate.toDateString() + " and " + lastSelectableDate.toDateString()
              });
          }
          return pass;
      }
      
      function dffs_PreSaveAction() {
          var dateSpanOK = checkDateSpan("DateColumn1", "DateColumn2");
          if(!dateSpanOK){
              // Prevent save
              return false;
          }
          return true;
      }

      Replace DateColumn1 and DateColumn2 with your start and end date fields.

      See if you can use this to build the logic you want in the form.

      To prevent adding two records in the same month you must use some other custom js to query the list for items created by the same user in the date-range that is allowed before it is saved. If you really need this (if your users actually will make duplicates) I can show you an example.

      Alexander

    • #31614
      First Eagle
      Participant

      Hello Alexander,

      Please assist again here, i’m implementing this code in my form, it’s preventing the form to save dates are out of range. but even when date are in the range the form doesn’t save and create new item in the backend list.

      To prevent adding two records in the same month you must use some other custom js to query the list for items created by the same user in the date-range that is allowed before it is saved. If you really need this (if your users actually will make duplicates) I can show you an example.

      R/ Please show me an example.

      Best regards

    • #31634
      Alexander Bautz
      Keymaster

      I’ll show you a code example when you have sorted out this first problem. You must ensure your datespan validates by either setting a debugger or alert i the dffs_PreSaveAction function, or by running this in the console (hit F12 > Console):

      checkDateSpan("DateColumn1", "DateColumn2");

      Please replace the field names to match yours and see if it return true or false.

      Alexander

Viewing 5 reply threads
  • You must be logged in to reply to this topic.