Getting Invalid date in On change CS. Please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 07:59 AM
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:
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 08:01 AM
Hi Mohith,
As I said, Start Date and End Date validations are completed. (Start date should not be after End Date)
Here is the code for that:
What I am looking for is, I want in the same coding standard for Start date.
(Start date should not be past date)
So, can you help me in this please?
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 11:56 AM
Hi @Lucky1
Script for Client Script defined for 'u_start_date'
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var currentDate = new Date();
// alert('currentDate = ' + currentDate);
var startDate = new Date(g_form.getValue('u_start_date'));
// alert('startDate = ' + startDate); // Gives valid date
if (startDate <= currentDate) {
// alert('Error start date must be after current date!');
g_form.addErrorMessage('Error start date must be after current date!');
g_form.clearValue('u_start_date');
// return false;
}
}
Script for Client Script defined for 'u_end_date':
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var startDate = new Date(g_form.getValue('u_start_date'));
var endDate = new Date(g_form.getValue('u_end_date'));
if (endDate < startDate) {
// alert('Error start date must be after current date!');
g_form.addErrorMessage('Error end date must be after start date!');
g_form.clearValue('u_end_date');
// return false;
}
}
You could use a 'Before' business rule defined on the table, and combine the logic above (modified for use in a BR). And abort the insert/update when either condition is true. Both of the above have been tested, but allows an empty value if the record is saved. a BR can prevent that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 01:35 PM
Hi @Lucky1,
If you want to use a Client Script, then I don't see any problem with yours. I'm assuming the u_start_date and u_end_date are defined as Date fields. I did a quick test, with the two fields defined as Date and not Date/Time fields, and the following onChange Client Script defined for u_start_date works:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var currentDate = new Date();
alert('currentDate = ' + currentDate);
var startDate = new Date(g_form.getValue('u_start_date'));
alert('startDate = ' + startDate); // Gives valid date
if (startDate <= currentDate) {
alert('Error start date must be after current date!');
g_form.clearValue('u_start_date');
// return false;
}
}
You can use the similar logic for a 2nd Client Script defined for the u_end_date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 05:29 PM
function onChangeEndDate() {
var startDate = g_form.getValue('u_start_date');
var endDate = g_form.getValue('u_end_date');
if (startDate && endDate) {
var startDateObj = new Date(startDate);
var endDateObj = new Date(endDate);
if (endDateObj <= startDateObj) {
g_form.setValue('u_end_date', ''); // Clear the invalid date
alert('End date should be greater than the start date.');
}
}
}