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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4 

How check if a date is valid?

Hello, 

How I can check if my date input is a valid date - not just a number in formatt like 02/29/2019 or 99/99/9999? 
I'm using an controller class in lightninc component.

 
Raj VakatiRaj Vakati
You can use javascript validation on the controller side  ..

update this code like 
  1. create an attribute that will show the error message when you enter the bad date 
 
<aura:attribute name="value" type="Date" default=""/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <ui:inputDate aura:id="Birthdate" label="Date Of Birth" 
                  value="{!v.value}" 
                  displayDatePicker="false" 
                  format="MM/dd/yyyy" blur="{!c.formatDoB}"/>

Check here
 
doInit : function(component, event, helper) {
        var today = new Date();
        component.set('v.value', today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate());
    },
    formatDoB: function(component, helper, event) {
         var dob = new Date(component.find("Birthdate").get("v.value"));
        console.log(dob);
If( dob.getMonth() <1 && dob.getMonth()>12){
// Though an Errro by Saying Date is not valide and Repeate same for the date and year
}
         
        
    },