the code below fails when i enter a date in the format 31/12/2015 on the line msg= ‘ 1502525 The date you entered is not valid: ‘ + dateField.value + ‘.’; and i have no idea why
[CODE]
function IsLeapYear(year) {
//Convert from string to number
year = year – 0;
if ((year/4) != Math.floor(year/4)) return false;
if ((year/100) != Math.floor(year/100)) return true;
if ((year/400) != Math.floor(year/400)) return false;
return true;
} //function IsLeapYear…
function countChar(str, daChar) {
//See how many of ‘dachar’ are in ‘str’ and return as daCount
var daCount=0;
for (i = 0; i < str.length; i++) {
if (str.charAt(i) == daChar)
//increment count each time we find daChar in str
daCount++;
}
return daCount;
} //function countChar…
function IsDateValid(dateField, noDateRqrd) {
//If no date is required and they didn’t enter one, then go back
if (((typeof noDateRqrd != “undefined”) && (dateField.value.length == 0))||dateField.value.length == 0)
return null;
//We’re going to check this date so go on
var GoodLength = false;
var msg=”;
var daysOfMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysOfMonthLY = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
//Make sure they have the right number of ‘/’
var cCount=countChar(dateField.value,’/’);
//force a date value
if (dateField.value.length >= 3) {
//alert(‘cc: ‘ + cCount + ‘fl: ‘ + dateField.value.length);
if ((dateField.value.length >= 3) && (cCount == 1) && (dateField.value.length <= 5))
GoodLength=true;
else if ( (dateField.value.length >= 4) && (cCount == 2) && (dateField.value.length <= 10))
GoodLength=true;
}
//If the length is good, then let’s check the contents of the dateField
if (GoodLength == true) {
var firstslash=dateField.value.indexOf(‘/’)+1; //must add 1 to get real position
var lastslash=dateField.value.lastIndexOf(‘/’)+1;
/****************************
Note: substr is not included in CFStudio so ‘substr’ will not be bolded & blue
*****************************/
var day=dateField.value.substr(0,firstslash-1);
if ( !isNaN(day) ){
if (firstslash != lastslash)
{
//alert(dateField.value);
var day=dateField.value.substr(firstslash,lastslash-firstslash-1); //lastslash-firstslash+1
//We only need to tell it where to cut off the first char
var year=dateField.value.substr(lastslash);
//Access dates can range from 100 to 9999.
//SQL Server dates can range from 1753 to 9999.
//Since this code can run for either db, we’ll use the more restrivtive date of 1753.
//The length test will catch any date > 9999.
if ( (month < 13) &&(year.length < 5))
{
//If there isn’t a year value, then put in the current year
if (year.length == 0) {
//There is no year so we only need to get day
var month=dateField.value.substr(firstslash,lastslash-firstslash-1);
//Since they didn’t enter a year, we’ll use the current year
var now = new Date();
year=now.getFullYear();
dateField.value = day + “/” + month + “/” + year;
} else if ((year.length == 1) || (year.length == 2)) {
var tmpDate = new Date(dateField.value);
//Programmer’s Note: entered value of “00” would be return as “1900” by getFullYear
tmpDate_yyyy = tmpDate.getFullYear()-1900; //Get 4 dig date and reindex without centuary
//alert(‘tmpDate_yyyy: ‘ + tmpDate_yyyy);
if (tmpDate_yyyy < 100) { //If less than 100, then we’re in 20th centuary
if (tmpDate_yyyy < 60) //do 00 to 59 as 20xx
tmpDate.setFullYear(2000 + tmpDate_yyyy); //reset year to 21st centuary
else //do 60 to 99 as 19xx
tmpDate.setFullYear(1900 + tmpDate_yyyy); //reset year to 20th centuary
//Have to do it the hard way or we’ll also get day, time, etc. which we don’t want here
//getMonth is zero based so it returns a number one less that we want
}
dateField.value = tmpDate.getDate() + “/” + tmpDate.getMonth()+1 +”/” + tmpDate.getFullYear();
} else if ((year.length == 4)&&(year > 1752)) {
dateField.value = day + “/” + month + “/” + year;
} else {
msg= ‘100000000000000 The date you entered is not valid: ‘ + dateField.value + ‘.’;
}//if
}
else
{
msg= ‘ 1502525 The date you entered is not valid: ‘ + dateField.value + ‘.’;
} //if
}
else {
//There is no year so we only need to get day
var month=dateField.value.substr(firstslash);
if (!isNaN(day)) {
//Since they didn’t enter a year, we’ll use the current year
var now = new Date();
var year=now.getFullYear();
dateField.value = day + “/” + month + “/” + year;
}
else {
msg= ‘130 The day you entered is not valid: ‘ + dateField.value + ‘.’;
} //if
}
//alert(‘m: ‘ + month + ‘ d: ‘ + day + ‘ y:’ + year);
if ((day==0)||(IsLeapYear(year) && day > daysOfMonthLY[month-1])||(!IsLeapYear(year) && day > daysOfMonth[month-1]))
msg= ‘140 The date you entered is not valid: ‘ + dateField.value + ‘.’;
}
else {
msg=’1500000 The month you entered is not valid: ‘ + month + ‘.’;
}
} else {
if (dateField.value.length == 0) {
msg= ‘16026515 You must enter enter a valid Date.’;
} else if ((dateField.value.toLowerCase() == ‘today’)||(dateField.value.toLowerCase() == ‘now’)) {
var now = new Date();
var month=now.getMonth()+1; //Have to add 1 to get correct month
var day=now.getDate();
var year=now.getFullYear();
dateField.value = day + “/” + month + “/” + year;
} else if ((dateField.value.indexOf(‘+’) == 0)||(dateField.value.indexOf(‘-‘) == 0)) {
//alert(‘in: ‘ + dateField.value + ‘, NaN: ‘ + isNaN(dateField.value));
if (!isNaN(dateField.value)) {
var now = new Date();
var nowMS = now.getTime(); //Get the current time in milliseconds
//Add the number of milliseconds in a day times the number of days requested
//Use the Math.floor to make sure we only get whole days in case they add a decimal value
now.setTime(nowMS + Math.floor((dateField.value * 1000*60*60*24)));
var month=now.getMonth()+1; //Have to add 1 to get correct month
var day=now.getDate();
var year=now.getFullYear();
dateField.value = day + “/” + month + “/” + year;
} else
msg= ‘170 The proper format, +#, or -#, with no spaces in between: e.g., “+6”, or “-2”. You entered: “‘ + dateField.value + ‘”.’;
} else
msg= ‘146: The date you entered is not valid: “‘ + dateField.value + ‘”.’;
}
if(msg.length > 0) {
if (typeof noDateRqrd == ‘undefined’) {
var lastLine=”180 Select ‘OK’ to re-enter value, or ‘Cancel’ to enter the current date.nn”
} else {
var lastLine=”190 Select ‘OK’ to re-enter value, or ‘Cancel’ to clear the date.nn”
}
var conmsg =”nn____________________________________________________nn” +
msg +
“nn____________________________________________________nn” +
“190 A valid date must contain at least one forward slash ‘/’ as well as a day and a month.n” +
“200 If you leave the year blank, then the current year will be inserted.nn” +
“210 The allowable date range is from 1/1/1753 to 12/31/9999.nn” +
lastLine
if (confirm(conmsg))
dateField.focus();
else {
if (typeof noDateRqrd == ‘undefined’) {
//get the current time
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
dateField.value= day + “/” + month + “/”+ year;
} else {
dateField.value=””;
}
}
return false;
} else
return true;
}