Below validation code works for date input of format dd/mm/yyyy or mm/dd/yyyy
function validateDateInput(varDate)
{
var flag=0;
var wrongEntryFlag=0;
var count=0;
if((varDate.charAt(1)=='/')||(varDate.charAt(3)=='/')||(varDate.charAt(2)=='/')||(varDate.charAt(5)=='/')||(varDate.charAt(4)=='/'))
{
var checkExpression="0123456789/";
for(var i=0;i<varDate.length;i++)
{
for(var j=0;j<checkExpression.length;j++)
{
flag=0;
if((varDate.charAt(i)==checkExpression.charAt(j)))
{
flag=1;
count++;
}
if(flag==1)
break;
}
}
}
if((count==8 || count==9 || count==10 ))
{
return true;
}
else
{
return false;
}
}
function validateDate(varDate)
{
if(validateDateInput(varDate))
{
var test= new Date();
var currentMonth = test.getMonth() + 1;
var currentDay= test.getDate();
var currentYear = test.getFullYear();
var varMonth;
var count;
var varDay;
var varYear;
var count2;
if(varDate.length==8)
{
varMonth=varDate.charAt(0);
varDay=varDate.charAt(2);
varYear=varDate.charAt(4)+varDate.charAt(5)+varDate.charAt(6)+varDate.charAt(7);
}
else if(varDate.length==10)
{
varMonth=varDate.charAt(0)+varDate.charAt(1);
varDay=varDate.charAt(3)+varDate.charAt(4);
varYear=varDate.charAt(6)+varDate.charAt(7)+varDate.charAt(8)+varDate.charAt(9);
}
else if(varDate.length==9 && varDate.charAt(1)=='/')
{
varMonth=varDate.charAt(0);
varDay=varDate.charAt(2)+varDate.charAt(3);
varYear=varDate.charAt(5)+varDate.charAt(6)+varDate.charAt(7)+varDate.charAt(8);
}
else if(varDate.length==9 && varDate.charAt(2)=='/')
{
varMonth=varDate.charAt(0)+varDate.charAt(1);
varDay=varDate.charAt(3);
varYear=varDate.charAt(5)+varDate.charAt(6)+varDate.charAt(7)+varDate.charAt(8);
}
if(varYear<currentYear)
{
alert("year should be greater than current year");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else if(varYear==currentYear && varMonth<currentMonth)
{
alert("month cannot be less than current month");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else if (varYear==currentYear && varMonth==currentMonth && varDay<currentDay )
{
alert("Date cannot be less than current date");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else if (varYear==currentYear && varMonth==currentMonth && varDay==currentDay )
{
alert("date cannot be today's date");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else
{
return true;
}
}
else
{
alert("Not a valid Date value.")
document.getElementById("diariseDate_date").value = "Date";
return false;
}
}
function validateDateInput(varDate)
{
var flag=0;
var wrongEntryFlag=0;
var count=0;
if((varDate.charAt(1)=='/')||(varDate.charAt(3)=='/')||(varDate.charAt(2)=='/')||(varDate.charAt(5)=='/')||(varDate.charAt(4)=='/'))
{
var checkExpression="0123456789/";
for(var i=0;i<varDate.length;i++)
{
for(var j=0;j<checkExpression.length;j++)
{
flag=0;
if((varDate.charAt(i)==checkExpression.charAt(j)))
{
flag=1;
count++;
}
if(flag==1)
break;
}
}
}
if((count==8 || count==9 || count==10 ))
{
return true;
}
else
{
return false;
}
}
function validateDate(varDate)
{
if(validateDateInput(varDate))
{
var test= new Date();
var currentMonth = test.getMonth() + 1;
var currentDay= test.getDate();
var currentYear = test.getFullYear();
var varMonth;
var count;
var varDay;
var varYear;
var count2;
if(varDate.length==8)
{
varMonth=varDate.charAt(0);
varDay=varDate.charAt(2);
varYear=varDate.charAt(4)+varDate.charAt(5)+varDate.charAt(6)+varDate.charAt(7);
}
else if(varDate.length==10)
{
varMonth=varDate.charAt(0)+varDate.charAt(1);
varDay=varDate.charAt(3)+varDate.charAt(4);
varYear=varDate.charAt(6)+varDate.charAt(7)+varDate.charAt(8)+varDate.charAt(9);
}
else if(varDate.length==9 && varDate.charAt(1)=='/')
{
varMonth=varDate.charAt(0);
varDay=varDate.charAt(2)+varDate.charAt(3);
varYear=varDate.charAt(5)+varDate.charAt(6)+varDate.charAt(7)+varDate.charAt(8);
}
else if(varDate.length==9 && varDate.charAt(2)=='/')
{
varMonth=varDate.charAt(0)+varDate.charAt(1);
varDay=varDate.charAt(3);
varYear=varDate.charAt(5)+varDate.charAt(6)+varDate.charAt(7)+varDate.charAt(8);
}
if(varYear<currentYear)
{
alert("year should be greater than current year");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else if(varYear==currentYear && varMonth<currentMonth)
{
alert("month cannot be less than current month");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else if (varYear==currentYear && varMonth==currentMonth && varDay<currentDay )
{
alert("Date cannot be less than current date");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else if (varYear==currentYear && varMonth==currentMonth && varDay==currentDay )
{
alert("date cannot be today's date");
document.getElementById("diariseDate_date").value = "Date";
return false;
}
else
{
return true;
}
}
else
{
alert("Not a valid Date value.")
document.getElementById("diariseDate_date").value = "Date";
return false;
}
}
No comments:
Post a Comment