The script is pretty big and I'm reasonably new to Javascript so I'm sure it could be written more efficiently. I looked for a script that was reasonably exhaustive as my users are impatient and don't want to worry about how they enter their dates, couldn't find exactly what I wanted so bastardised a few scripts and came up with this
It'll take pretty much any input (1.1.4, 24*DEC*2001, 010104 etc) and validate it before returning DD-MON-YYYY (I used it in an Oracle form). It should be easy to change this to whatever output you're after.
Hope it's useful...
Joe.
function isDateorNull(theElement, theElementName) {
var CorrectDate;
var SplitDate;
var thisDay;
var thisMonth;
var thisYear;
var inpDate = theElement.value;
var DayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var MonthArray = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
var CockUp = 0;
// kill if nothing there
if (inpDate.length == 0 ) return true;
// numerical dates first for DDMMYYYY OR DDMMYY
if (/^[0-9]{3}$/.test(inpDate.substr(1,3))) {
if (/^[0-9]{8}/.test(inpDate)) {thisDay = inpDate.substr(0,2); thisMonth = inpDate.substr(2,2); thisYear = inpDate.substr(4,4);}
else if (/^[0-9]{6}/.test(inpDate)) {thisDay = inpDate.substr(0,2); thisMonth = inpDate.substr(2,2); thisYear = inpDate.substr(4,2);}
else {CockUp = 1;}
}
// now alpha numeric dates for DDMONYYYY, DMONYYYY, DDMONYY, DMONYY, DDMONY, DMONY or MONYY
else if (/^[a-zA-Z|0-9]{3}$/.test(inpDate.substr(1,3))) {
}
// lastly pick up every conceivable special character or kick out with error
else {
if (/-/.test(inpDate)) {SplitDate = inpDate.split("-");}
else if (/\//.test(inpDate)) {SplitDate = inpDate.split("/");}
else if (/\./.test(inpDate)) {SplitDate = inpDate.split(".");}
else if (/,/.test(inpDate)) {SplitDate = inpDate.split(",");}
else if (/\;/.test(inpDate)) {SplitDate = inpDate.split(";");}
else if (/\:/.test(inpDate)) {SplitDate = inpDate.split(":");}
else if (/\*/.test(inpDate)) {SplitDate = inpDate.split("*");}
else if (/_/.test(inpDate)) {SplitDate = inpDate.split("_");}
else if (/%/.test(inpDate)) {SplitDate = inpDate.split("%");}
else {CockUp = 1;}
thisDay = SplitDate[0];
thisMonth = SplitDate[1].toUpperCase();
thisYear = SplitDate[2];
}
// request reinput data if nonexistent
if (!thisYear) {CockUp = 1;}
if (!thisMonth) {CockUp = 1;}
if (!thisDay) {CockUp = 1;}
// kick out any errors before we do any formatting etc
if (CockUp == 1) {
alert("Your input date format is incorrect or contains invalid characters - please try again using DD-MON-YYYY");
theElement.focus();
return false;
}
// change from numeric to alpha-numeric
if (/^[0-9]{1}$/.test(thisMonth.substr(0,1))) {
switch (thisMonth) {
case "01" : case "1" : thisMonth = 'JAN'; break;
case "02" : case "2" : thisMonth = 'FEB'; break;
case "03" : case "3" : thisMonth = 'MAR'; break;
case "04" : case "4" : thisMonth = 'APR'; break;
case "05" : case "5" : thisMonth = 'MAY'; break;
case "06" : case "6" : thisMonth = 'JUN'; break;
case "07" : case "7" : thisMonth = 'JUL'; break;
case "08" : case "8" : thisMonth = 'AUG'; break;
case "09" : case "9" : thisMonth = 'SEP'; break;
case "10" : thisMonth = 'OCT'; break;
case "11" : thisMonth = 'NOV'; break;
case "12" : thisMonth = 'DEC'; break;
default : alert("Your input month is incorrect, it should be between 1 and 12"); theElement.focus(); return false;
}
}
// sort variable lengths out and build date
if (thisDay.length == 1) {thisDay = "0" + thisDay;}
if (thisYear.length == 1) {thisYear = "200" + thisYear;}
if (thisYear.length == 2) {if (thisYear >= 50) {thisYear = "19" + thisYear; } else {thisYear = "20" + thisYear;}}
CorrectDate = thisDay + "-" + thisMonth + "-" + thisYear;
// Check alpha-numeric Valid Month
var filter=/JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC/;
if (! filter.test(thisMonth)) {
alert("Please enter a Correct Month");
theElement.focus();
return false;
}
// Check For Leap Year
N=thisYear;
if ( ( N%4==0 && N%100 !=0 ) || ( N%400==0 ) ) {
DayArray[1]=29;
}
// check for correct days in month
for(var ctr=0; ctr<=11; ctr++){
if (MonthArray[ctr]==thisMonth) {
if (thisDay<= DayArray[ctr] && thisDay >0 ) {
CockUp = 0;
} else {
alert("Your day input is incorrect for the month in question");
theElement.focus();
return false;
CockUp = 1;
}
}
if (CockUp == 0) {
theElement.value = CorrectDate;
}
}
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.