validation for my catalog item variables - dates cant be more than 14 days apart
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
I am trying to prepare a validation for my catalog item variables.
I have 2 variable named: 'paternity_start_date' and 'paternity_leave_end_date'
I want to have 2 validations:
1. End date cant be before start date
2. period cant be longer than 14 days. Leave can be shorter, but cant exceed 14 days (14 days included).
Would you be able to assist me here?
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I normally use catalog client script which is more recommended to do the validations, but you can also use UI policy and script.
Type: onChange (or onSubmit) Applies to: paternity_start_date and paternity_leave_end_date variables
Example:
function onSubmit() {
var start = g_form.getValue('paternity_start_date');
var end = g_form.getValue('paternity_leave_end_date');
if (!start || !end) {
return true; // Don't validate if one field is empty
}
var startDate = new Date(start);
var endDate = new Date(end);
// 1. End date cannot be before start date
if (endDate < startDate) {
g_form.showFieldMsg('paternity_leave_end_date', 'End date cannot be before start date.', 'error');
return false;
}
// 2. Duration cannot exceed 14 days
var diffDays = (endDate - startDate) / (1000 * 60 * 60 * 24);
if (diffDays > 14) {
g_form.showFieldMsg('paternity_leave_end_date', 'The leave period cannot exceed 14 days.', 'error');
return false;
}
return true;
}
ɪꜰ ᴍʏ ᴀɴꜱᴡᴇʀ ʜᴀꜱ ʜᴇʟᴘᴇᴅ ᴡɪᴛʜ ʏᴏᴜʀ Qᴜᴇꜱᴛɪᴏɴ, ᴘʟᴇᴀꜱᴇ ᴍᴀʀᴋ ᴍʏ ᴀɴꜱᴡᴇʀ ᴀꜱ ᴛʜᴇ ᴀᴄᴄᴇᴘᴛᴇᴅ ꜱᴏʟᴜᴛɪᴏɴ ᴀɴᴅ ɢɪᴠᴇ ᴀ ᴛʜᴜᴍʙꜱ ᴜᴘ.
ʙᴇꜱᴛ ʀᴇɢᴀʀᴅꜱ
ꜱʀᴇᴇʀᴀᴍ