Important Update: Archer Community Scheduled Maintenance on November 23–24 - New Community Launching Soon! Learn More..
2024-10-15 04:38 PM
Hi All,
We have one requirement where customer want to restrict/Disable the weekend dates within date field. Is there any custom object to fulfil the above requirement.
Regards,
NT Admin
2024-10-15 11:36 PM
This doesn't actually disable the dates on the date picker itself, but if you choose a weekend on the picker it will erase the selection and pop up a warning message. Based it off another custom object we use for validating date being greater/less than each other. (Comparing fields to each other - Archer Community - 481513 (archerirm.community))
Just update the field IDs in the dateFields variable with your field IDs, can add as many as you want.
<script type="text/javascript">
// Array of date field IDs
var dateFields = [31763, 33171];
// Attach change event handlers to date fields after the application loads
Sys.Application.add_load(function() {
dateFields.forEach(function(fieldId) {
// Attach change event to each date field
$('div[id*="f' + fieldId + 'cdp"]').change(function() {
checkWeekend(fieldId); // Check if the selected date is a weekend
});
});
});
// Function to check if the selected date is a weekend
function checkWeekend(fieldId) {
// Get the date value from the input field
var dateVal = $('div[id*="f' + fieldId + 'cdp"]' + ' > input:nth-child(1)').val();
var dateObj = convertToDate(dateVal); // Convert the date string to a Date object
if (dateObj != null && !isNaN(dateObj.getTime())) {
var day = dateObj.getDay(); // Get the day of the week
if (day === 2 || day === 3) { // 2 = Saturday, 3 = Sunday
var msg = "The date cannot be on a weekend.";
var title = 'Warning';
WarningAlert(msg, title); // Show a warning alert
nullThisID(fieldId); // Clear the date field
}
}
}
// Function to clear the value of the date field
function nullThisID(fieldId) {
$('div[id*="f' + fieldId + 'cdp"]' + ' > input:nth-child(1)').val('');
}
// Function to convert a date string to a Date object
function convertToDate(aDate) {
var dataSplit = aDate.split("/");
var returnDate = null;
if (dataSplit.length == 3) {
returnDate = new Date(dataSplit[2],dataSplit[1],dataSplit[0]);
}
return returnDate;
}
</script>
2024-10-16 09:25 AM
Thank you, Jordan, for your response. I just tested this Script, it is not working for saturdays.
2024-10-16 12:25 PM
I did tweak the code a bit, it is working now. here is the updated code. Thank you!
<script type="text/javascript">
// Array of date field IDs
var dateFields = [48584];
// Attach change event handlers to date fields after the application loads
Sys.Application.add_load(function() {
dateFields.forEach(function(fieldId) {
// Attach change event to each date field
$('div[id*="f' + fieldId + 'cdp"]').change(function() {
checkWeekend(fieldId); // Check if the selected date is a weekend
});
});
});
// Function to check if the selected date is a weekend
function checkWeekend(fieldId) {
// Get the date value from the input field
var dateVal = $('div[id*="f' + fieldId + 'cdp"]' + ' > input:nth-child(1)').val();
var dateObj = convertToDate(dateVal); // Convert the date string to a Date object
debugger
if (dateObj != null && !isNaN(dateObj.getTime())) {
var day = dateObj.getDay(); // Get the day of the week
if (day === 0|| day === 6) { // 6= Saturday, 0= Sunday
var msg = "The date cannot be on a weekend.";
var title = 'Warning';
WarningAlert(msg, title); // Show a warning alert
nullThisID(fieldId); // Clear the date field
}
}
}
// Function to clear the value of the date field
function nullThisID(fieldId) {
$('div[id*="f' + fieldId + 'cdp"]' + ' > input:nth-child(1)').val('');
}
// Function to convert a date string to a Date object
function convertToDate(aDate) {
var dataSplit = aDate.split("/");
var returnDate = null;
if (dataSplit.length == 3) {
returnDate = new Date(dataSplit[2],dataSplit[0]-1,dataSplit[1]);
}
return returnDate;
}
</script>
2024-10-16 06:05 PM
Glad to hear you managed to fix it up. I was surprised for my environment that 2 was a saturday and 3 was a sunday, instead of 6 and 0. Unsure why it's different for my environment compared to yours, but as long as it works!