Last active
September 5, 2024 12:55
-
-
Save faisalahammad/b80c26ce27a80e24115db83bbad25932 to your computer and use it in GitHub Desktop.
A simple code snippet to disable Sundays and prevent past dates in the Ninja Forms Advanced Datepicker add-on. This solution uses Flatpickr to ensure only future dates can be selected and Sundays are excluded. Perfect for booking forms or scheduling systems where past dates and Sundays aren't valid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Ninja Forms - Disable Past Dates and Sundays in Advanced Datepicker | |
* @author Faisal Ahammad | |
*/ | |
jQuery(document).ready(function ($) { | |
function initCustomDatePicker() { | |
var customDatePicker = Marionette.Object.extend({ | |
initialize: function () { | |
this.listenTo(Backbone.Radio.channel("flatpickr"), "init", this.modifyDatepicker); | |
}, | |
modifyDatepicker: function (dateObject, fieldModel) { | |
var currentDate = new Date(); | |
currentDate.setDate(currentDate.getDate() + 1); // Allow only future dates | |
// Disable Sundays | |
dateObject.set("disable", [ | |
function (date) { | |
return date.getDay() === 0; | |
} | |
]); | |
dateObject.set("minDate", currentDate); | |
}, | |
}); | |
new customDatePicker(); | |
} | |
// Form ID (185) to apply the code on a specific form | |
if ($("#nf-form-185-cont").length) { | |
initCustomDatePicker(); | |
} else { | |
var observer = new MutationObserver(function (mutations, observerInstance) { | |
if ($("#nf-form-185-cont").length) { | |
initCustomDatePicker(); | |
observerInstance.disconnect(); | |
} | |
}); | |
observer.observe(document.body, { childList: true, subtree: true }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment