Tuesday 5 May 2015

Restrict User To Select Past Date Using Custom Date Picker

In salesforce standard date picker we can not restrict user to select past date so we need to write validation .
Below code will help to restrict user to select past dates without validation .

//VF pages
<apex:page id="pg" controller="CustomDatePicker">
<head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
 </head>  
    <script>
       $(function(){
        $('.datepicker').datepicker({ minDate: 0 }).on('changeDate', function(ev){
            $('#sDate1').text($('#datepicker').data('date'));
            $('#datepicker').datepicker('hide');
        });
 
    })
    </script>
    <apex:form id="frm">
        <apex:inputText id="datepicker" styleClass="datepicker"  value="{!currentDateValue}"/>
        <apex:commandButton action="{!getValue}" value="getvalue"/>
    </apex:form>
 </apex:page>

 //Apex class
 public class CustomDatePicker {
    public String currentDateValue { get; set; }
    public void getValue(){
      System.debug('*************'+currentDateValue);
    }
}
//Below script will help you to get date in different format
<script>
       $(function(){
        $('.datepicker').datepicker({minDate: 0 ,dateFormat: 'dd/mm/yy' }).on('changeDate', function(ev){
            $('#sDate1').text($('#datepicker').data('date'));
            $('#datepicker').datepicker('hide');
        });
 
    })
</script>

I will suggest to down load the JQuery Librery from below link and add in static resource and use in page .

http://jquery.com/download/



No comments:

Post a Comment