Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active June 22, 2024 21:12
Show Gist options
  • Save Qubadi/ee5cea9a0187f8dd9e75bfb16ac9f1ec to your computer and use it in GitHub Desktop.
Save Qubadi/ee5cea9a0187f8dd9e75bfb16ac9f1ec to your computer and use it in GitHub Desktop.
Jetformbuilder custom code to disable weekend date/days ( Date field )
1. First use the Datefield widget, and name it: date_field.
2. Then create a page to add your form, exemple: contact
3. A nonce for security is added.
4. Dont forget to add your page slug name in the code here:
function disable_weekends_in_datepicker() {
// Only load this script on the 'contact' page
if ( is_page('contact') ) {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
var dateField = document.getElementById('date_field');
if (dateField) {
// Function to check if the date is a weekend
function isWeekend(date) {
var day = date.getDay();
return day === 0 || day === 6; // 0 is Sunday, 6 is Saturday
}
// Event listener for date selection
dateField.addEventListener('input', function() {
var selectedDate = new Date(this.value);
if (isWeekend(selectedDate)) {
alert('Weekend dates are not selectable.');
this.value = ''; // Clear the field
}
});
// Preventing the input from showing weekend dates initially
var currentDate = new Date();
if (isWeekend(currentDate)) {
dateField.value = ''; // Clear the field if today is a weekend
}
}
});
</script>
<?php
// Add a nonce for security
wp_nonce_field('disable_weekends_nonce', 'disable_weekends_nonce');
}
}
add_action('wp_footer', 'disable_weekends_in_datepicker');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment