Below is the javascript code to validate date
function ChkDate(year) {
var str=year.split('/');
if(str.length!=3)return false;
var date=parseInt(str[0]);
if(isNaN(date))return false;
if(date==0){
date=parseInt(str[0].substring(1,2))
}
var m=parseInt(str[1]);
if(m==0){
m=parseInt(str[1].substring(1,2))
}
if(isNaN(m))return false;
var year=parseInt(str[2]);
if(isNaN(year))return false;
var daysInMonth = DaysArray(12)
// var m=monNumFor(month);
var s=new Date().toString();
var Arr=s.split(' ');
for(var i=3;i<Arr.length;i++)
{
if(Arr[i].length==4)
{
Arr[3]=Arr[i];
}
}
Arr[2]=parseInt(Arr[2]);//date
Arr[1] =parseInt(monNumFor(Arr[1]));//mon
Arr[3]=parseInt(Arr[3]);//year
var minYear=Arr[3]-100;
var maxYear=Arr[3]+100;
if (date<1 || date>31 || (m ==2&& date>daysInFebruary(year)) || date > daysInMonth[m]){
return false
}
if(m<1 || m>12) return false;
if (year==0 || year<minYear || year>maxYear){
return false
}
if (year>Arr[3])
{
return false;
}
if(year==Arr[3] && m>Arr[1])
{
return false;
}
if(year==Arr[3] && m==Arr[1] && date>Arr[2])
{
return false;
}
return true;
}
function daysInFebruary (year){
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function monNumFor(m)
{
switch(m)
{
case "Jan":
m ="1";
break;
case "Feb":
m ="2";
break;
case "Mar":
m ="3";
break;
case "Apr":
m ="4";
break;
case "May":
m ="5";
break;
case "Jun":
m ="6";
break;
case "Jul":
m ="7";
break;
case "Aug":
m ="8";
break;
case "Sep":
m ="9";
break;
case "Oct":
m ="10";
break;
case "Nov":
m ="11";
break;
case "Dec":
m ="12";
break;
}
return m;
}
function ChkDate(year) {
var str=year.split('/');
if(str.length!=3)return false;
var date=parseInt(str[0]);
if(isNaN(date))return false;
if(date==0){
date=parseInt(str[0].substring(1,2))
}
var m=parseInt(str[1]);
if(m==0){
m=parseInt(str[1].substring(1,2))
}
if(isNaN(m))return false;
var year=parseInt(str[2]);
if(isNaN(year))return false;
var daysInMonth = DaysArray(12)
// var m=monNumFor(month);
var s=new Date().toString();
var Arr=s.split(' ');
for(var i=3;i<Arr.length;i++)
{
if(Arr[i].length==4)
{
Arr[3]=Arr[i];
}
}
Arr[2]=parseInt(Arr[2]);//date
Arr[1] =parseInt(monNumFor(Arr[1]));//mon
Arr[3]=parseInt(Arr[3]);//year
var minYear=Arr[3]-100;
var maxYear=Arr[3]+100;
if (date<1 || date>31 || (m ==2&& date>daysInFebruary(year)) || date > daysInMonth[m]){
return false
}
if(m<1 || m>12) return false;
if (year==0 || year<minYear || year>maxYear){
return false
}
if (year>Arr[3])
{
return false;
}
if(year==Arr[3] && m>Arr[1])
{
return false;
}
if(year==Arr[3] && m==Arr[1] && date>Arr[2])
{
return false;
}
return true;
}
function daysInFebruary (year){
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function monNumFor(m)
{
switch(m)
{
case "Jan":
m ="1";
break;
case "Feb":
m ="2";
break;
case "Mar":
m ="3";
break;
case "Apr":
m ="4";
break;
case "May":
m ="5";
break;
case "Jun":
m ="6";
break;
case "Jul":
m ="7";
break;
case "Aug":
m ="8";
break;
case "Sep":
m ="9";
break;
case "Oct":
m ="10";
break;
case "Nov":
m ="11";
break;
case "Dec":
m ="12";
break;
}
return m;
}
No comments:
Post a Comment