Date Validation - client script

Nipan1
Tera Contributor

Hi,

 

I have requirements as below: 

 

1. 'Expected Start Date' should be selected after 7 days from today (if not then show error message and clear the value)

2. 'Expected Completion Date' should be after 'Expected Start Date'. (if not then show error message and clear the value)

 

For the first requirement: I created a UI Policy which is working fine. 

Nipan1_0-1713216476328.pngNipan1_1-1713216496028.png

 

 

For the second requirement:  I created a client script, which is getting Invalid Date from Start date in the script and not working. 

Nipan1_2-1713216633899.pngNipan1_3-1713216686938.png

 

 

Can anyone please help me fix the above issue. 

 

Thanks,

@Nipan1 

 

1 ACCEPTED SOLUTION

Deborah Brown L
Kilo Sage

Hi @Nipan1 ,

For the second requirement also you can create a UI Policy and add the below condition.

DeborahBrownL_0-1713243377883.pngDeborahBrownL_1-1713243413231.png

 

date_2 should be "Expected Completion Date" and date should be "Expected Start Date".

 

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me.

Regards,
Deborah Brown

View solution in original post

8 REPLIES 8

BalaG
Kilo Sage

Hello @Nipan1  is there a reason you are not using UI policy for your second requirement? Here is a screenshot showing how I implement that on the change record.

 

Screenshot 2024-04-15 at 3.39.21 PM.png

 

If  this helped please mark it 👍 helpful or  solution at appropriate

--

Bala Guthy

Sumanth16
Kilo Patron

Hi @Nipan1 ,

 

1. 'Expected Start Date' should be selected after 7 days from today (if not then show error message and clear the value)

 

If you use UI Policy -> with condition Start date is not Empty && End Date - Less than - 7 - days - after -Start date and script as below 

function onCondition() {
    g_form.addErrorMessage('End Date should be 7 days after start date');
    g_form.clearValue('u_end_date');
}

Problem  : g_form.showFieldMsg may not be work and error message remains on form. 

 

If you use Onchange() Client script you can show g_form.showFieldMsg under field which looks good. try below code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var start = new Date(g_form.getValue('u_start_date'));
    var end = new Date(g_form.getValue('u_end_date'));
    var sevenDaysLater = new Date(start.getTime() + 7 * 24 * 60 * 60 * 1000);
    if (end < sevenDaysLater) {
		g_form.showFieldMsg('u_end_date','End date should be after 7 days of start date','error',false);
        g_form.setValue('u_end_date', '');
    }

}

 

2. 'Expected Completion Date' should be after 'Expected Start Date'. (if not then show error message and clear the value)

 

 

 

Create an onChange script for your start_date and end_date fields.

//start_date onChange Function
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
var nowDate = new Date(); //Creates a JS date object for right now
var checkStart = new Date(newValue); // Create a JS Date object using the desired start_date field (from newValue parameter)

if (nowDate >= checkStart){ //JS Date objects compared
alert('You cannot select today or a day in the past');
g_form.setValue('start_date','');
}
}

 

//end_date onChange Function
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
var startDate = new Date(g_form.getValue('start_date')); //get the value of the start_date field using the g_form.getValue method
var checkEnd = new Date(newValue); // create a new JS date from the desired end_date field (using newValue parameter)

if (checkEnd <= startDate){ //Again - a simple JS Date object comparison
alert('End date must be after start date');
g_form.setValue('end_date','');
}
}

Nipan1
Tera Contributor

Hi @Sumanth16,

 

I tried the above solution you mentioned, but they are not working. Still getting invalid dates while checking the dates in the scripts. 

Deborah Brown L
Kilo Sage

Hi @Nipan1 ,

For the second requirement also you can create a UI Policy and add the below condition.

DeborahBrownL_0-1713243377883.pngDeborahBrownL_1-1713243413231.png

 

date_2 should be "Expected Completion Date" and date should be "Expected Start Date".

 

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me.

Regards,
Deborah Brown