How to ensure only future (or past) dates can be submitted to a date component of a Drupal Webform.

Category: drupal

This is where we need to get a little funky with the 'Additional Validation' field under 'Webform Advanced Settings'.

First of all, find out the name of the date field you are wanting to validate. You can do this via the database (look for it in the webform_component table and note it's form_key field), or, the preferred route is to use the Firefox Web Developer toolbar, enable 'Display Form Details' and note down the name(s) of the inputs.

Go to the form, click on 'edit', scroll down to the 'Webform Advanced Settings' and expand.

Examine, modify and then insert this little bit of code:-


$preferred_date = mktime("00", "00", "00",
if ($preferred_date < time())
form_set_error('submitted][preferred_travel_date', t('Your date is in the past,
please select a valid travel date'));

Note, in this instance, the date picker is the three field type with day, month, year drop downs. Also, in this instance, the date picker is embedded in a fieldset 'destination', so remove or rename this according to your installation.

Finally, note the "$preferred_date < time()" clause, reverse the < to > if you're wanting dates only in the past.


Less and Equal

How do you make the date to include the today date. I am adding the equal sign (=) to the right of the more sigh (<) and it does not work for me. It works otherwise just fine, but I need the today's day as well. Please help. Thanks you very much for the help.

Nice! Just what I was looking

Nice! Just what I was looking for! Thanks!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.