1
2
3/*
4 * source :sfdcmonkey.com
5 * 12/26/2017
6*/
7({
8 /*call dateUpdate function on onchange event on date field*/
9 dateUpdate : function(component, event, helper) {
10
11 var today = new Date();
12 var dd = today.getDate();
13 var mm = today.getMonth() + 1; //January is 0!
14 var yyyy = today.getFullYear();
15 // if date is less then 10, then append 0 before date
16 if(dd < 10){
17 dd = '0' + dd;
18 }
19 // if month is less then 10, then append 0 before date
20 if(mm < 10){
21 mm = '0' + mm;
22 }
23
24 var todayFormattedDate = yyyy+'-'+mm+'-'+dd;
25 if(component.get("v.myDate") != '' && component.get("v.myDate") < todayFormattedDate){
26 component.set("v.dateValidationError" , true);
27 }else{
28 component.set("v.dateValidationError" , false);
29 }
30 },
31
32 submit : function(component,event,helper){
33 // get the 'dateValidationError' attribute value
34 var isDateError = component.get("v.dateValidationError");
35
36 if(isDateError != true){
37 alert('date is valid.. write your more logic here...');
38 }
39 }
40})
41
42
43
1function validatedate(inputText,DateFormat)
2{
3// 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"/" or dot"." or dash"-" in the dates formats
4var invalid = "";
5var dt = "";
6var mn = "";
7var yr = "";
8var k;
9var delm = DateFormat.includes("/") ? "/" : ( DateFormat.includes("-") ? "-" : ( DateFormat.includes(".") ? "." : "" ) ) ;
10var f1 = inputText.split(delm);
11var f2 = DateFormat.split(delm);
12for(k=0;k<=2;k++)
13{
14 dt = dt + (f2[parseInt(k)]=="dd" ? f1[parseInt(k)] : "");
15 mn = mn + (f2[parseInt(k)]=="mm" ? f1[parseInt(k)] : "");
16 yr = yr + (f2[parseInt(k)]=="yyyy" ? f1[parseInt(k)] : "");
17}
18var mn_days = "0-31-" + (yr % 4 == 0 ? 29 : 28) + "-31-30-31-30-31-31-30-31-30-31";
19var days = mn_days.split("-");
20if( f1.length!=3 || mn.length>2 || dt.length>2 || yr.length!=4 || !(parseInt(mn)>=1 && parseInt(mn)<=12) || !(parseInt(yr)>=parseInt(1900) && parseInt(yr)<=parseInt(2100)) || !(parseInt(dt)>=1 && parseInt(dt)<=parseInt(days[parseInt(mn)])) )
21{
22 invalid = "true";
23}
24alert( ( invalid=="true" ? "Invalid Date" : "Valid Date") );
25}