This solution provides a professional date selection experience for your Elementor forms without the need for premium plugins.
Elementor, Websites, WordPress customization

Customizing Date Selection in Elementor Forms

This solution provides a professional date selection experience for your Elementor forms without the need for premium plugins.

The Problem with Elementor Form Date Fields

If you’ve ever built forms with Elementor, you’ve likely encountered a significant limitation: by default, Elementor’s date fields don’t allow you to set date restrictions. This becomes problematic when you need to:

  • Prevent users from selecting dates in the past
  • Limit bookings to a specific future timeframe (e.g., next 6 months)
  • Ensure dates are within a valid service window

While there are premium plugins available to solve this issue, this article provides a completely free solution using custom JavaScript that anyone can implement, even without advanced coding knowledge.

The Solution: Custom Flatpickr Configuration

The following code solves the Elementor date field limitations by customizing the underlying Flatpickr date picker. This solution will:

  1. Set a minimum date of tomorrow (preventing past or same-day selections)
  2. Set a maximum date of 180 days in the future
  3. Work with the default English language interface

Step-by-Step Implementation Guide

Step 1: Copy the Code

Here’s the complete, working code that you can use:

				
					jQuery(document).ready(function($) {
  // Function to modify Flatpickr configurations
  function modifyDatePicker() {
    const dateInput = document.getElementById('form-field-field_6230bde');
    if (dateInput && dateInput._flatpickr) {
      // Calculate minimum date (today + 1 day)
      const today = new Date();
      const minDate = new Date(today);
      minDate.setDate(today.getDate() + 1);
      
      // Calculate maximum date (today + 180 days)
      const maxDate = new Date(today);
      maxDate.setDate(today.getDate() + 180);
      
      // Set up English localization (default)
      if (typeof window.flatpickr !== 'undefined') {
        // Note: English is the default language in Flatpickr
        // We're keeping this section for reference if you want to change languages later
        
        // Destroy the current instance
        dateInput._flatpickr.destroy();
        
        // Recreate with the correct settings
        flatpickr(dateInput, {
          dateFormat: "m/d/Y", // Standard US format: month/day/year
          minDate: minDate,
          maxDate: maxDate,
          disableMobile: "true",
          // Using default English locale
          altInput: true,
          altFormat: "m/d/Y", // Alternative format visible to the user
          onChange: function(selectedDates, dateStr, instance) {
            console.log("Selected date:", dateStr);
          }
        });
      } else {
        console.error("Flatpickr is not available in the window object");
      }
      
      console.log('Datepicker successfully modified:');
      console.log('- Minimum date: ' + minDate.toDateString());
      console.log('- Maximum date: ' + maxDate.toDateString());
      console.log('- Default English localization applied');
      console.log('- Date format: MM/DD/YYYY');
    }
  }

  // Function to use MutationObserver to detect when the datepicker is initialized
  function setupObserver() {
    const dateInput = document.getElementById('form-field-field_6230bde');
    if (!dateInput) {
      // If the element doesn't exist yet, try again soon
      setTimeout(setupObserver, 500);
      return;
    }
    
    // Check if the datepicker is already initialized
    if (dateInput._flatpickr) {
      modifyDatePicker();
      return;
    }
    
    // Configure an observer to monitor changes to the element
    const observer = new MutationObserver(function(mutations) {
      for (let mutation of mutations) {
        if (dateInput._flatpickr) {
          modifyDatePicker();
          observer.disconnect(); // Stop observing once the datepicker has been modified
          break;
        }
      }
    });
    
    // Observe the element's attributes and its children
    observer.observe(dateInput, {
      attributes: true,
      childList: true,
      subtree: true,
      attributeFilter: ['class']
    });
    
    // As a fallback, also try using setTimeout with increasing intervals
    const checkIntervals = [500, 1000, 2000, 3000, 5000];
    checkIntervals.forEach(interval => {
      setTimeout(function() {
        if (dateInput._flatpickr) {
          modifyDatePicker();
          observer.disconnect();
        }
      }, interval);
    });
  }
  
  // Start the process at the DOMContentLoaded event
  document.addEventListener('DOMContentLoaded', function() {
    setupObserver();
  });
  
  // Also start at the load event for safety
  window.addEventListener('load', function() {
    setupObserver();
  });
  
  // Backup: also try at the elementor/frontend/init event
  $(window).on('elementor/frontend/init', function() {
    elementorFrontend.hooks.addAction('frontend/element_ready/form.default', function() {
      setTimeout(setupObserver, 100);
    });
  });
});
				
			

Step 2: Customize the Form Field ID

The most important change you’ll need to make is to update the form field ID to match your specific Elementor form:

  1. In Elementor, edit your form
  2. Click on your date field
  3. Look for the “ID” field in the left panel, or inspect element using your browser’s developer tools
  4. Replace form-field-field_6230bde in the code with your own field ID
				
					const dateInput = document.getElementById('YOUR-FIELD-ID-HERE');
				
			

Step 3: Adjust Date Range (Optional)

If you need a different date range than tomorrow to 180 days in the future, modify these lines:

				
					// For minimum date (default is tomorrow)
minDate.setDate(today.getDate() + 1);

// For maximum date (default is 180 days from today)
maxDate.setDate(today.getDate() + 180);
				
			

Step 4: Date Format (Optional)

The code currently uses the US date format (MM/DD/YYYY). If you need a different format:

				
					// Change this for the date stored in the form
dateFormat: "m/d/Y",

// Change this for the date displayed to the user
altFormat: "m/d/Y",
				
			

Common formats:

  • US: m/d/Y (MM/DD/YYYY)
  • European: d/m/Y (DD/MM/YYYY)
  • ISO: Y-m-d (YYYY-MM-DD)

Step 5: Add the Code to Your Website

You have several options to add this code to your website:

  1. Option 1: Custom HTML Widget

    1. Add an Elementor HTML widget to your page
    2. Paste the code inside <script> tags:

 

<script>
// Paste the entire code here
</script>

Option 2: Code Snippets Plugin

  1. Install a free plugin like “Code Snippets”
  2. Create a new snippet
  3. Paste the code
  4. Set it to load on the frontend
  5. Save and activate

Option 3: Theme’s Custom JavaScript Section

Many themes have a section in the Customizer or theme options for custom JavaScript:

  1. Go to Appearance → Customize or Theme Options
  2. Find the Custom JavaScript/Scripts section
  3. Paste the code
  4. Save changes

How This Solution Works

This code works by:

  1. Detecting When the Date Picker is Ready: Using multiple methods to ensure it works regardless of when elements load
  2. Customizing the Date Picker: Setting minimum and maximum dates and localization
  3. Ensuring Reliability: With fallback mechanisms if the initial attempt fails

The code is specifically designed to work with Elementor forms without requiring any third-party plugins or advanced configuration.

Troubleshooting

If the date picker isn’t being modified:

  1. Check the Console: Open your browser’s developer tools (F12) and look for errors
  2. Verify Field ID: Double-check that you’re using the correct field ID from your form
  3. Timing Issues: If your form loads dynamically, you might need to adjust the timing values:
				
					const checkIntervals = [500, 1000, 2000, 3000, 5000, 7000, 10000];
				
			

Important Note About Languages

While this example uses English (the default language in Flatpickr), the library supports many languages. Flatpickr has an extensive built-in localization system that can be implemented with just a few code changes.

For reference, Flatpickr supports over 40 languages including French, German, Spanish, Chinese, Japanese, Arabic, and many more. Implementing different languages will be covered in a future article.

Conclusion

This solution provides a professional date selection experience for your Elementor forms without the need for premium plugins. The code is robust, handles multiple scenarios, and can be easily customized to fit your specific needs.

By implementing this solution, you’ll ensure users can only select valid dates within your specified range, improving both user experience and data quality in your form submissions. The most critical step is correctly identifying your form’s date field selector and applying this JavaScript to your site.

Need help with this solution or looking for custom development?

Visit my Stay In Touch page to connect and discuss your project. Discover a range of web development services and specialized WordPress solutions tailored to your needs. Let's work together to enhance your digital presence.