[SalesForce] Disable Future Dates datepicker

So I would like to disable for user to choose future date in datepicker

<apex:inputfield value="{!currService.csord__Activation_Date__c}" required="true"/>

Thank you for the help !

Best Answer

You can do this with JQuery UI Datepicker [documentation], so this is essentially a duplicate of this question on StackOverflow.

Specifically, you will probably want to specify maxDate [documentation]. You can use more complicated blacklisting/whitelisting schemes by defining a beforeShowDay method [documentation] but that seems like overkill for your requirements. You will need to use apex:inputText instead and bind it to your controller differently.

Controller:

public String daySelected { get; set; }
public String dateSelected
{
    get
    {
        return Date.valueOf(day); // add try/catch somewhere, but not in your getter
    }
}
public SObject myRecord
{
    get
    {
        return new Custom_Object(Custom_Date__c = dateSelected);
    }
}

VisualForce:

<apex:stylesheet value="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<apex:stylesheet value="https://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" />

<apex:inputText styleClass="customDatePicker" value="{!daySelected }" />

<script src="https://code.jquery.com/jquery-1.11.2.min.js" />
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js" />
<script>
    (function (w, $) {
        "use strict";
        $('.customDatePicker').datepicker([
            maxDate: new Date()
        ]);
    })(window, jQuery.noConflict());
</script>