Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Seeking JavaScript assistance on AEM Form date picker

Avatar

Level 2

Hi Community,

 

I am seeking help on writing a piece of JavaScript to limit date picker and only allow selection from today + 7 days, as I am told the only way is putting code in rule editor.

 

Without the IT knowledge and resource, I turned to ChatGPT for help, but it didn't work.

 

Therefore, I want to find out what's wrong with the JavaScript ChatGPT gave me. Pasting them below

 

(I clicked copy selector and got this, then replaced ".datepicker" with it: #guideContainer-rootPanel-guidedatepicker__)

 

First try:

Brian_Chau_CLP_2-1708393464235.png

(function ($) {
    $(document).on("dialog-ready", function () {
        // Get the date picker widget
        var datePicker = Granite.UI.Foundation.Utils.getDatePicker($("#guideContainer-rootPanel-guidedatepicker__"));

        // Get the current date
        var currentDate = new Date();

        // Calculate the date four days from now
        var futureDate = new Date(currentDate.getTime() + (4 * 24 * 60 * 60 * 1000));

        // Set the minimum and maximum date values of the date picker
        datePicker.setDisabledDates({
            from: currentDate.toISOString(),
            to: futureDate.toISOString()
        });
    });
})(Granite.$);

Second try:

Brian_Chau_CLP_0-1708393188728.png

document.addEventListener("DOMContentLoaded", function () {
  var datePicker = document.querySelector("#guideContainer-rootPanel-guidedatepicker__");

  datePicker.addEventListener("coral-datepicker:ready", function () {
    var currentDate = new Date();

    var futureDate = new Date();
    futureDate.setDate(currentDate.getDate() + 4);

    datePicker.min = formatDate(currentDate);
    datePicker.max = formatDate(futureDate);
  });

  function formatDate(date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, "0");
    var day = String(date.getDate()).padStart(2, "0");
    return `${year}-${month}-${day}`;
  }
});

 

I also don't understand this error

Brian_Chau_CLP_3-1708393578518.png

 

Thanks so much in advance!

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 10

Can you please try to use var in place of let as es6 not working for you

(function (document, $) {

    
    $(document).on("click", ".cq-dialog-submit", function(e) {
        
        var currentDate = new Date();
        var selectedDate = new Date($("[name='./date']").val());

        // Calculate date difference
        var timeDifference = selectedDate.getTime() - currentDate.getTime();

        // Calculating the no. of days between
        // two dates
        var numberOfDays =Math.round(timeDifference / (1000 * 3600 * 24));

        // To display the final no. of days (result)
        console.log("Number of days - " + numberOfDays);
       
        if (numberOfDays > 7) {
       	    var ui = $(window).adaptTo("foundation-ui");
            ui.alert("Error", "Date must be less than or equals to 7 days.", "error");
            return false;
        }
    });
   
})(document, Granite.$);

View solution in original post

5 Replies

Avatar

Level 10

We can add tooltip and try for below code also to show error on dialog submit to have 7 days


 

 

(function (document, $) {

    
    $(document).on("click", ".cq-dialog-submit", function(e) {
        
        let currentDate = new Date();
        let selectedDate = new Date($("[name='./date']").val());

        // Calculate date difference
        let timeDifference = selectedDate.getTime() - currentDate.getTime();

        // Calculating the no. of days between
        // two dates
        let numberOfDays =Math.round(timeDifference / (1000 * 3600 * 24));

        // To display the final no. of days (result)
        console.log("Number of days - " + numberOfDays);
       
        if (numberOfDays > 7) {
       	    var ui = $(window).adaptTo("foundation-ui");
            ui.alert("Error", "Date must be less than or equals to 7 days.", "error");
            return false;
        }
    });
   
})(document, Granite.$);

 

 

imran__khan_1-1708400760545.png

 

 

Avatar

Level 2

Hi @Imran__Khan , Thank you for your advice! I think it is a great solution for my use case.

 

I tried it; however, I got this error.

"Let is available in ES6 (use 'esversion: 6') or Mozilla JS extension (use moz)"

Brian_Chau_CLP_0-1708403169922.png

Would there be a substitute of 'let'?

Also, may I ask what event do you choose to trigger it? I picked initialized.

Brian_Chau_CLP_1-1708403336577.png

Many thanks,

Brian

Avatar

Correct answer by
Level 10

Can you please try to use var in place of let as es6 not working for you

(function (document, $) {

    
    $(document).on("click", ".cq-dialog-submit", function(e) {
        
        var currentDate = new Date();
        var selectedDate = new Date($("[name='./date']").val());

        // Calculate date difference
        var timeDifference = selectedDate.getTime() - currentDate.getTime();

        // Calculating the no. of days between
        // two dates
        var numberOfDays =Math.round(timeDifference / (1000 * 3600 * 24));

        // To display the final no. of days (result)
        console.log("Number of days - " + numberOfDays);
       
        if (numberOfDays > 7) {
       	    var ui = $(window).adaptTo("foundation-ui");
            ui.alert("Error", "Date must be less than or equals to 7 days.", "error");
            return false;
        }
    });
   
})(document, Granite.$);

Avatar

Level 2

Hi @Imran__Khan, may I seek further help from you.

 

It seems that any selected date triggers the validation error.

Brian_Chau_CLP_0-1709004989926.png

Today is 27 Feb

Brian_Chau_CLP_1-1709005160890.png

 

 Wondering what is wrong in the script.