The CreatorCon Call for Content is officially open! Get started here.

Prevent user from selecting date within 30 days of current date

Valerie24
Tera Contributor

I'm trying to create an onChange Catalog Client Script that will display a modal pop up when someone enters a date that is within 30 days from the current date. I've tried using a number of scripts posted on community but they are not working for me. We do not want to use a UI Policy/Action for this. Any help with a script to achieve this would be greatly appreciated!

 

  • Variable = u_correction_begin_date 
  • If u_correction_begin_date is within 30 days of current date they should receive a modal pop up indicating "you cannot submit a date within 30 days of today."
  • If u_correction_begin_date is 30 days or more, no pop up should appear. 

 

Thank you in advanced!

1 ACCEPTED SOLUTION

Vengadesh
Tera Guru

Hi @Valerie24 ,

 

Create an onchange client script and try the below code 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    var currentDate = new Date();
    var selectedDate = new Date(g_form.getValue('u_correction_begin_date'));
 
    // Calculate the difference in days
    var timeDiff = selectedDate - currentDate;
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Convert milliseconds to days
 
    // Check if the selected date is within 30 days of the current date
    if (parseInt(diffDays)<30 && parseInt(diffDays)>=0) {
       alert('Please select a date that is at least 30 days from today.');
        g_form.clearValue('u_correction_begin_date'); // Optionally clear the field
    }
 
}

 

Try this and let me know if it works

Regards

Vengadesh

View solution in original post

7 REPLIES 7

Vengadesh
Tera Guru

Hi @Valerie24 ,

 

Create an onchange client script and try the below code 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    var currentDate = new Date();
    var selectedDate = new Date(g_form.getValue('u_correction_begin_date'));
 
    // Calculate the difference in days
    var timeDiff = selectedDate - currentDate;
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Convert milliseconds to days
 
    // Check if the selected date is within 30 days of the current date
    if (parseInt(diffDays)<30 && parseInt(diffDays)>=0) {
       alert('Please select a date that is at least 30 days from today.');
        g_form.clearValue('u_correction_begin_date'); // Optionally clear the field
    }
 
}

 

Try this and let me know if it works

Regards

Vengadesh

Valerie24
Tera Contributor

This works perfectly! Thank you so much!

Valerie24
Tera Contributor

@Vengadesh ,

 

Is there a way to update the script to also prevent users from entering past dates (i.e. cannot enter 5/21/2024 or 4/25/2024)?

 

Thank you in advanced!

Hi @Valerie24 ,

 

You can configure a onChange Client script on your date field which has to be validated and give the script given below

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var currentDate = new Date();
    currentDate.setHours(0, 0, 0, 0); // Set time to 00:00:00 to compare only dates
    
    // Get the value of the date field (replace 'your_date_field' with the actual field name)
    var dateField = newValue;
    var enteredDate = new Date(dateField);
    
    // Compare the dates
    if (enteredDate<currentDate) {
		g_form.clearValue("Your field name");
        alert('You cannot enter a past date.');
    }
}

 

 

Give the name of your field as the parameter inside clearValue() method.

 

If you find this helpful, mark it as helpful.

 

Thank you!