2

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

1 Answer 1

2

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);
    }
}
1
  • I tried formatting the date format to "DD/MM/YYYY" but it doesn't work. Puzzled why only your "YYYY-MM-DD" works.
    – compski
    Commented Jan 10, 2020 at 7:28

You must log in to answer this question.

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