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"/" or dot"." or dash"-" in the dates formats
var invalid = "";
var dt = "";
var mn = "";
var yr = "";
var k;
var delm = DateFormat.includes("/") ? "/" : ( DateFormat.includes("-") ? "-" : ( DateFormat.includes(".") ? "." : "" ) ) ;
var f1 = inputText.split(delm);
var f2 = DateFormat.split(delm);
for(k=0;k<=2;k++)
{
dt = dt + (f2[parseInt(k)]=="dd" ? f1[parseInt(k)] : "");
mn = mn + (f2[parseInt(k)]=="mm" ? f1[parseInt(k)] : "");
yr = yr + (f2[parseInt(k)]=="yyyy" ? f1[parseInt(k)] : "");
}
var mn_days = "0-31-" + (yr % 4 == 0 ? 29 : 28) + "-31-30-31-30-31-31-30-31-30-31";
var days = mn_days.split("-");
if( 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)])) )
{
invalid = "true";
}
alert( ( invalid=="true" ? "Invalid Date" : "Valid Date") );
}
Thank you!
8
0
Are there any code examples left?
New code examples in category Other