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

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 Jan 10 '20 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.