function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul Chauhan 33Rahul Chauhan 33 

I want to apply validate on Lightning Input date field. How can i achieve this..??

My requirement is to apply validation for lightning Input date filed that user can select only today or yesterday date ..??
How can i achieve this 

Thnks in advance..!!
<lightning:input aura:id="yesterday" class="slds-size_1-of-4"  name="Work Day" type="date" label="Workday" 
  max="{!v.today}"   messageWhenBadInput="You cannot fill old date time entry"  value="{!v.work}"  format="DD-MM-YYYY" required="true" />


Js controller:
 var pastday =component.find("v.yesterday");
       var fordate =pastday.get("v.value");
       if(pastday< $today.getDate()-1){
          pastday.set("v.errors", [{message:" You cannot fill time sheet of previous day  "}]);
        }

 
Raj VakatiRaj Vakati
You can able to do it like below
 
<aura:component>
    <!--create myDate aura attribute for store date field value-->  
    <aura:attribute name="myDate" type="date" />
    <!--create dateValidationError boolean attribute for show error msg on invalid selection
      and disable submit button -->    
    <aura:attribute name="dateValidationError" type="boolean" />
    
    
    <div class="slds-p-around_medium">
        
        <lightning:input class="{! v.dateValidationError ? 'slds-has-error' : ''}"
                         type="date"
                         label="Renewal Date"
                         value="{!v.myDate}"
                         name="date"
                         onchange="{!c.dateUpdate}" />
        
        <aura:if isTrue="{!v.dateValidationError}">
            <div class="slds-text-color_error slds-p-left_x-small">
                Date must be in present or in future..
            </div>
        </aura:if>    
        
        <br/>
        
        <lightning:button disabled="{!v.dateValidationError}" variant="brand" onclick="{!c.submit}" label="Submit"></lightning:button>
        
    </div>       
</aura:component>
 
({
   /*call dateUpdate function on onchange event on date field*/ 
    dateUpdate : function(component, event, helper) {
        
        var today = new Date();        
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is 0!
        var yyyy = today.getFullYear();
     // if date is less then 10, then append 0 before date   
        if(dd < 10){
            dd = '0' + dd;
        } 
    // if month is less then 10, then append 0 before date    
        if(mm < 10){
            mm = '0' + mm;
        }
        
     var todayFormattedDate = yyyy+'-'+mm+'-'+dd;
        if(component.get("v.myDate") != '' && component.get("v.myDate") < todayFormattedDate){
            component.set("v.dateValidationError" , true);
        }else{
            component.set("v.dateValidationError" , false);
        }
    },
    
    submit : function(component,event,helper){
      // get the 'dateValidationError' attribute value
        var isDateError = component.get("v.dateValidationError");
        
        if(isDateError != true){
            alert('date is valid.. write your more logic here...');
        }
    }
})

Refer this link

http://sfdcmonkey.com/2017/12/26/validation-past-date-lightning-component/​​​​​​​