Validate two date fields and make field visible and mandatory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 07:31 AM
Hi All,
I have a requirement to validate two date fields when requestor fills the form if the second date field is more than 2 days of first date field a new field should get visible and make mandatory to fill the reason. How to do this and please do provide script required for this .
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 09:28 AM
Hi @Kruthik M Shiva,
Please try this below code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var firstDate = g_form.getValue('first_date_field');
var secondDate = g_form.getValue('second_date_field');
var thirdField = g_form.getControl('third_field');
// Convert date strings to Date objects
var firstDateObj = new Date(firstDate);
var secondDateObj = new Date(secondDate);
// Calculate the difference in days
var timeDiff = Math.abs(secondDateObj.getTime() - firstDateObj.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
// If the difference is more than 2 days, show and make mandatory the third field
if (diffDays > 2) {
thirdField.setAttribute('mandatory', true);
g_form.showFieldMsg('third_field', 'Please provide a reason for the difference.', 'info');
} else {
thirdField.removeAttribute('mandatory');
g_form.clearMessages(' third_field');
}
}
Please accept my solution if it resolves your issue and thumps 👍 up
Thanks
Jitendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 06:44 PM
Hi Jitendra,
Thanks for the response I will check the script and Implement I will be accepting once it worked.
Thanks,
Kruthik Shivaprasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 02:09 AM
Hi @Kruthik M Shiva ,
Try below code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if ( newValue === '') {
g_form.setMandatory('u_reason', false);
g_form.setDisplay('u_reason', false);
return;
}
// Get the values of start_date and end_date fields
var startDate = g_form.getValue('u_start_date');
var endDate = g_form.getValue('u_end_date');
// Ensure both dates are filled
if (startDate && endDate) {
// Convert to Date objects
var start = new Date(startDate);
var end = new Date(endDate);
// Calculate the difference in days
var timeDiff = end - start;
var daysDiff = timeDiff / (1000 * 3600 * 24);
// Check if the difference is more than 2 days
if (daysDiff > 2) {
// Show and make the reason field mandatory
g_form.setDisplay('u_reason', true);
g_form.setMandatory('u_reason', true);
}else{
g_form.setMandatory('u_reason', false);
g_form.setDisplay('u_reason', false);
}
}
}
I have tested it in my PDI and it is working fine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 02:09 AM
For demo I have created 3 fields on incident form - Start Date, End Date and Reason
Result -
Scenario 1 - When Form loads and start and end date is empty
Scenario 1 - When date diff is less than 2 days
As date diff is less than 2 days . the reason field will be hidden
Scenario 3 - when date difference is more than 2 days
Even if you changed the end date later and if then date diff is less than 2 days then it will be hidden again
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik