/    Sign up×
Community /Pin to ProfileBookmark

Changing the code below to reflect and validate euro date instead of US date

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;
}

[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoOct 16.2015 — Holy moley. All that just to validate a date? Have you considered using a date picker instead?
Copy linkTweet thisAlerts:
@Kevin2Oct 16.2015 — "210 The allowable date range is from 1/1/1753 to [B][COLOR="#FF0000"]12/31/9999[/COLOR][/B].nn" +
On New Year's Eve 9999 will they be "partying like it's 1999"? And which day of the week is that? I need to plan! ?

Seriously, do you really need to validate a date almost 8 [B][I]millennia[/I][/B] in the future? It's also interesting that you require dd/mm/yyyy but December 31, 9999 is mm/dd/yyyy in the above code. Oops.

Here's something a little more practical with a lot less code. It sort of pre-validates the date so the user can't make a mistake and get yelled at for entering a year 13 millennia from now. The date output is also in the format you require.

[code=html]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>date 'validation'</title>
<script>
function days(el,y){
var s = document.getElementById('short'),
f_n = document.getElementById('feb_n'),
f_ly = document.getElementById('feb_ly');
switch (el.value){
case '04': // 30 day months
case '06':
case '09':
case '11':
s.setAttribute("disabled", "");
f_n.removeAttribute("disabled", "");
f_ly.removeAttribute("disabled", "");
break;
case '02': //February options
switch (y){
case '2016': // leap years, add to as necessary
case '2020':
s.setAttribute("disabled", "");
f_n.setAttribute("disabled", "");
f_ly.removeAttribute("disabled", "");
break;
default: // non leap years
s.setAttribute("disabled", "");
f_n.setAttribute("disabled", "");
f_ly.setAttribute("disabled", "");
break;
}
break;
default: // 31 day months
s.removeAttribute("disabled", "");
f_n.removeAttribute("disabled", "");
f_ly.removeAttribute("disabled", "");
break;
}
}
function makeDate(m,d,y,n) { //formats the date the way you want it. could also be done with server-side scripting.
n.value = d + "/" + m + "/" + y;
}
</script>
</head>
<body>

<form>
<label>Year:
<select id="year" name="year">
<option value="2014">2014</option>
<option value="2015" selected>2015</option> <!-- "selected" could be added to year options via scripting -->
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select></label><br>
<label>Month:
<select id="month" name="month" onchange="days(this,year.value)">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select></label><br>
<label>Day:
<select id="day" name="day">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29" id="feb_ly">29</option>
<option value="30" id="feb_n">30</option>
<option value="31" id="short">31</option>
</select></label><br>

<input type="text" id="ndate" name="ndate"><br> <!-- should probably be type="hidden" for form purposes -->
<br>
<input type="button" onclick="makeDate(month.value,day.value,year.value,ndate)" value="create date">
</form>
</body>
</html>[/code]
×

Success!

Help @sajhahelp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...