1

I want to create a date picker in salesforce in which i can only select date less then today date, i can't select date after today date, how i can achieve this task in salesforce.?

3 Answers 3

2

You can do this with jQuery Datepicker set the maxDate property to 0 so that it will disable future dates. Hope this helps.

<apex:page id="myPage" standardController="Contact">
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"></link>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <apex:form id="myForm">
      Select Date: <apex:inputText id="sDate"/>
      <script type="text/javascript">
          var $j = jQuery.noConflict();
              $j("#myPage\\:myForm\\:sDate").datepicker({
                  inline: true,
                  maxDate: 0  
              });
      </script>
    </apex:form>
</apex:page>
3
  • Your approach is good i knows this approach but problem is this if we want to select 1980 then we need to more and more click approx 12*35 = 420 click to go there so i think is very bed thing. Another approach to go on 1980 to enter date by hand but user don't want to do this , user always select a date Using date picker . So if you have any other idea then give me thanks in Advance. Commented Oct 2, 2015 at 3:08
  • Thank you very much i have solved it using your code and add some extra attribute in it like. changeMonth: true, changeYear: true, maxDate: 0, inline: true, yearRange: "-80:+0", Commented Oct 2, 2015 at 3:18
  • @RavikantKedia If this works for you, can you please mark it as answer. It will help others.
    – Saroj Bera
    Commented Oct 2, 2015 at 4:24
1

You should use <apex:inputField> for getting the date picker. But for the validation, you should use controller method to determine whether the picked date is less than today, if not throw error message to user.

if(object__c.field__c > system.today()) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Please select date less than today'));
}

You need to add <apex:pageMessages> component in your VF page as well to show the message added in controller.

Hope is helps.

1

As Vigneshwaran rightly said that you will have to create and <apex:inputField> in your visualforce.

I think this will work when you bind the <apex:inputField> with an object field which is of date or date-time type (or you will have to create a dummy field) also it will not limit you from selecting past date. (The limit will be imposed in form of an error after user submit or you will have to write an on-change function on the field)

Alternatively, you may jQuery Datepicker which supports limiting the date value and has many other options as well.

  <script>
  $(function() {
    $( "#datepicker" ).datepicker({ minDate: 0, maxDate: "+1M +10D" });
  });
  </script>

There are many datpickers available apart from this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .