Gravity Forms restricting days in the date picker

This short tutorial is for Gravity Forms users, specifically those willing to get involved in a little code!

WordPress Tech
wordpress-code-examples

This short tutorial is for Gravity Forms users. We use this plugin extensively for our project work.

When working with the Gravity Forms date picker you sometimes have to get fancy with eliminating days so that a user can not choose them.

In this situation we needed to disable Saturdays, why would you want to do anything on a Saturday?

The Gravity Forms Date Picker is using jQuery so you can interact with it as the page loads.

This kind of script belongs in a script tag, could be in your Javascript files that are included in your page load, or perhaps if you’re using Genesis as your framework you can put this in the scripts field that allows this on a page by page basis.

As you can see in the screenshot, Saturday; they are no longer an option.

Enjoy!

Gravity Forms Date Picker
function noSaturdays(date) {
	var weekday=new Array(7);
	weekday[0]="Sunday";
	weekday[1]="Monday";
	weekday[2]="Tuesday";
	weekday[3]="Wednesday";
	weekday[4]="Thursday";
	weekday[5]="Friday";
	weekday[6]="Saturday";
	//console.log(weekday[date.getDay()]);
	if(weekday[date.getDay()] == 'Saturday') {
		return [false, '', 'Not available Saturdays'];
        }else if(weekday[date.getDay()] == 'Saturday') {
                 return [false, '', 'Not available Saturdays'];
        }

	return [true];
}
jQuery(window).load(function(){
	jQuery('.hasDatepicker').datepicker('option', 'beforeShowDay', noSaturdays);
});

Reader Interactions

Comments

  1. Mr. Hinz says

    Hi Peter, thanks for your code-snippet. Why do we need the if and else-if? :

    if (weekday[date.getDay()] == ‘Saturday’) {
    return [false, ”, ‘Not available Saturdays’];
    } else if(weekday[date.getDay()] == ‘Saturday’) {
    return [false, ”, ‘Not available Saturdays’];
    }

    Both have the same condition:
    weekday[date.getDay()] == ‘Saturday’

    Thanks in advance.
    Diego

Leave a Reply

Your email address will not be published. Required fields are marked *