Date Validation in Javascript. Example using split and Regex for dd/mm/yyyy, mm/dd/yyyy format

There are many ways to check whether the input date is valid or not using Javascript. Let me explain the two ways. You can use any one of the ways which one you feel comfortable.

                For validating the date, first part will ensure the given date is in right format (dd/mm/yyyy or mm/dd/yyyy) that means, the given date string should have two slashes only and the remaing characters should be a digit(numeric). Next part will ensure the digits (day, month, year) should be in valid range which means days should be in the range 1 to 31 (February- 28 days, for leap year 29 days), month should be in the range 1 to 12. In our example, isValidDate() function will accept date either in dd/mm/yyyy format or mm/dd/yyyy format. Change the logic accordingly as given below. In the first method, we are splitting the dd, mm, yyyy using substring method. isDigit () funtion is used to validate the dd, mm, yyyy is numeric or not and we have written logic to check the day, month, year is in the valid range.

             In the Second method, Date is split using Regular expression. After splitting the date, new Date(yyyy, mm-1, dd) is used to form date. Now getDate(date), getMonth(date), getYear(date) is used to check the day, month or year is valid by comparing dd with the result of getDate(date), mm with the result of getMonth(date) and yyyy with the result of getYear(date). If day, month, year is valid means, the date is also valid.

function submitForm()
{ 
	var dob=window.document.empForm.dob.value;
	if (isValidDate1(dob)==false) alert ("Please enter valid date");
   		document.empForm.dob.focus();
	if (isValidDate(dob)==false) alert ("Please enter valid date");
  		document.empForm.dob.focus();
 
}
//First Method to validate date   
function isValidDate(strDate)
{
 	var dmy=strDate;
 	if(dmy.length !=10)  return false;
 	var dd = dmy.substring(0,2);
 	var mm = dmy.substring(3,5);
 // if date format is mm/dd/yyyy , then  comment above 2 lines  & write  var mm = dmy.substring(0,2); var dd = dmy.substring(3,5);
 	var yyyy = dmy.substring(6);
 	var slash1 = dmy.substring(2,3);
 	var slash2 = dmy.substring(5,6);
 	if(slash1 != '/' || slash2 != '/') return false;
 	if (!isDigit(dd) ||  !isDigit(mm) || !isDigit(yyyy)) return false;
    var leap = (yyyy % 4 ==0 && yyyy % 100!=0) || (yyyy % 400==0);
 	if(mm==2 && leap!=true)
 		{
  			if  (dd==31 || dd==30 || dd==29) return false;
  		}
 	if(dd<0 || dd>31) return false; 
 	if(mm<0 || mm>12) return false; 
 	if (mm==2 && (dd==31 || dd==30)) return false;
 	if((mm==4) ||(mm==6) ||(mm==9) ||(mm==11))
 		{
  		if(dd==31) return false;
 		}
 	return true;
} 

function isDigit(strDigit){
    return /^\d+$/.test(strDigit);
}
function isValidDate1(inputDate){
	var dateFormat=/^\d{1,2}[\/]\d{1,2}[\/]\d{1,4}$/;
	if (!dateFormat.test(inputDate))
	{
		alert("Given Date Format is not in dd/mm/yyyy or mm/dd/yyyy.");
		return false;
	}
	else{
		var splitArr=inputDate.split("/");
		var dd=splitArr[0];
		var mm=splitArr[1];
	// if date format is mm/dd/yyyy , then  comment above 2 lines  &amp; write  var mm=splitArr[0];  var dd=splitArr[1];
		var yyyy=splitArr[2];
		var date = new Date(yyyy, mm-1, dd);
		if (((date.getDate()!=dd) || date.getMonth()+1!=mm)||(date.getYear()!=yyyy)) 
			{
			alert("Invalid Date . Please check the range of day , month , year"); 
			return false;
			}
		}
	return true;
	}


Leave a Reply