Date Validation

 
 
/*
 * source :sfdcmonkey.com 
 * 12/26/2017
*/
({
   /*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...');
        }
    }
})
 
 

4.13
8

                                    function validatedate(inputText,DateFormat)
{
// format dd/mm/yyyy or in any order of (dd or mm or yyyy) you can write dd or mm or yyyy in first or second or third position ... or can be slash&quot;/&quot; or dot&quot;.&quot; or dash&quot;-&quot; in the dates formats
var invalid = &quot;&quot;;
var dt = &quot;&quot;;
var mn = &quot;&quot;;
var yr = &quot;&quot;;
var k;
var delm = DateFormat.includes(&quot;/&quot;) ? &quot;/&quot; : ( DateFormat.includes(&quot;-&quot;) ? &quot;-&quot; : ( DateFormat.includes(&quot;.&quot;) ? &quot;.&quot; : &quot;&quot; ) ) ;
var f1 = inputText.split(delm);
var f2 = DateFormat.split(delm);
for(k=0;k&lt;=2;k++)
{ 
 dt = dt + (f2[parseInt(k)]==&quot;dd&quot; ? f1[parseInt(k)] : &quot;&quot;);
 mn = mn + (f2[parseInt(k)]==&quot;mm&quot; ? f1[parseInt(k)] : &quot;&quot;);
 yr = yr + (f2[parseInt(k)]==&quot;yyyy&quot; ? f1[parseInt(k)] : &quot;&quot;);
}
var mn_days = &quot;0-31-&quot; + (yr % 4 == 0 ? 29 : 28) + &quot;-31-30-31-30-31-31-30-31-30-31&quot;;
var days = mn_days.split(&quot;-&quot;);
if( f1.length!=3 || mn.length&gt;2 || dt.length&gt;2 || yr.length!=4 || !(parseInt(mn)&gt;=1 &amp;&amp; parseInt(mn)&lt;=12) || !(parseInt(yr)&gt;=parseInt(1900) &amp;&amp; parseInt(yr)&lt;=parseInt(2100)) || !(parseInt(dt)&gt;=1 &amp;&amp; parseInt(dt)&lt;=parseInt(days[parseInt(mn)])) )
{
 invalid = &quot;true&quot;;
}
alert( ( invalid==&quot;true&quot; ? &quot;Invalid Date&quot; : &quot;Valid Date&quot;)  );
}

4.13 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source