Getting Invalid date in On change CS. Please help

Lucky1
Tera Guru

Hi all,

 

I have start date (u_start_date) and End date (u_end_date) on a catalog item.

I have created an On change Client script on Start date field. I want to show an error message if the user selects today's date or old date, but in the alert message it is showing Invalid date.

Can you please help me here also, I want to write another On change CS, where End date should be greater than Start date. can you help me please?

 

My Script:

Lucky1_0-1693321136264.png

 

Regards,

Lucky

 

8 REPLIES 8

Jaspal Singh
Mega Patron
Mega Patron

Hi,

Try adding below between line 10 and 11

var today_date_time_str = formatDate(startDate, g_user_date_time_format);

 

Hi Jaspal,

 

I am done with writing code for End date should be greater than Start date.

Now my query is:

Start Date should not be Past days

For this I have written script but it's not working.

Can you please correct it?

Lucky1_0-1693369329615.png

 

 

Regards,

Lucky

Hello @Lucky1 DID YOU CHECK MY ABOVE RESPONSE FOR THE SAME .

YOU DONT NEED TO WRITE SCRIPT FOR THIS 

 

1) Create a UI policy with condition as "start date is today " or "start date is less than today" and in the run script  section  execute if true you can write an addErrorMessage as

 

function onCondition() {
g_form.addErrorMessage('your_message');
g_form.clearValue('u_start_date');
return false;
}

 Mark the answer correct if this helps you 

Thanks

Mohith Devatte
Tera Sage
Tera Sage

Hello @Lucky1 ,

No need to write a client script for this .

You can handle this via UI policies.

 

1) Create a UI policy with condition as "start date is today " or "start date is less than today" and in the run script  section  execute if true you can write an addErrorMessage as

 

function onCondition() {
g_form.addErrorMessage('your_message');
g_form.clearValue('u_start_date');
return false;
}

 

2)Create an onchange client script like below 

unction onChange(control, oldValue, newValue, isLoading) {
 
if (isLoading || newValue == '') {
 
return;
 
}
 
//get the entered date string
var startDate = g_form.getValue('u_start_date');
var endDate = g_form.getValue('u_end_date');
var startDateValue = new Date(startDate).valueOf();
var endDateValue = new Date(endDate).valueOf();
//Compare the two numbers
if (startDateValue > endDateValue) {
alert('End date must be after start date.');
g_form.setValue('u_end_date','');
}

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks