[SalesForce] How to set Date field to not exceed today’s date in lightning component

I have a requirement where I need Birthdate field on contact to validate so that it does not exceeds today's date.
Below is the code:

<lightning:input  value="{!v.objContact.Birthdate}" label="Date of Birth"/>

Can anyone let me know how to achieve this with javascript validation or any other method.

Thanks

Best Answer

You should add these in your codes;

//Component

<lightning:input type="date" value="{!v.objContact.Birthdate}" label="Date of Birth" onchange="{!c.dateController}"/>

//JS Controller

 dateController : function(component, event, helper){
    var today = $A.localizationService.formatDate(new Date(), "YYYY-MM-DD");
    var dateValue = component.get("v.objContact.Birthdate");
    console.log(today);

    if(dateValue > today){
        alert('You can not enter grater than today!');
        dateValue = null;
        component.set("v.objContact.Birthdate", dateValue);
    }
}
Related Topic