codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
This 17 message thread spans 2 pages:  < <   1  [2] 
  Re: IsDate() in JavaScript?  postonce at 22:18 on Thursday, June 14, 2007
 

!isNaN((new Date(1/1/05)).getYear()) won't work because the Date() constructor is too generous. For example alert(new Date("02/31/2007")) = March 3rd and even 3/3000/3 is considered an acceptable date (Wed May 17 1911).

  Re: IsDate() in JavaScript?  fred.watrous at 12:38 on Friday, May 07, 2010
 

function isISODate (value) {
var dates = value.split("-");
if (dates.length >= 3) {
var ds = dates[1] + "-" + dates[2] + "-" + dates[0];
var test = new Date (ds);
if (isNaN(test)) {
alert('Enter a valid date in the correct format: YYYY-MM-DD');
return false;
} else {
if (test.getFullYear() != (dates[0] * 1) || (test.getMonth()+1) != (dates[1] * 1) || test.getDate() != (dates[2] * 1)) {
alert('This is not a valid date: ' + value);
return false;
} else {
return true;
}
}
} else {
alert('Enter a valid date in the format: YYYY-MM-DD');
return false;
}
}

An ISO date is in the format YYYY-MM-DD, but javascript doesn't read that format. The string "ds" rearranges the date to MM-DD-YYYY. The getMonth() function returns the month as an integer from 0-11, hence "+1". Multipling the variable "dates" by 1 converts it into an integer.

This rutin checks that the format is right, that the position of the year, month and day is correct, and that the numbers given correspond to the intended date, since the javascript "date" function will add or remove days as appropriate in order to adjust to the closest possible date of that intended.

Fred

This 17 message thread spans 2 pages:  < <   1  [2] 







CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Incrementing a date field using javascript via prompt
•  Re: How to know the Control Type in javascript
•  Re: Get all elements with a common name
•  Re: PHP Session Management and Password Protection Class
•  Random Password Generator.. problem in file handling
•  Re: Difference between two dates including From & To date
•  Re: dereferenceing a string to a constant name
•  Re: PHP swapping images
•  Re: JavaScript: Not working in Firefox 3.x


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010