Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
inet
active_desktop
active_scrptng
asp.components
asp.db
asp.general
comctl32
comp.packaging
components.dev
dbweb
dhtml_editing
docobjects
html_authoring
html_objmodel
iis
iis.ftp
iis.security
iis.smtp_nntp
indexserver
misc
mshtml_hosting
scripting.jscript
scripting.vbscript
sdk_setup
shell_objmodel
urlmonikers
webbrowser_ctl
wininet
  
 
date: Mon, 16 Jun 2008 16:52:19 -0700,    group: microsoft.public.inetsdk.programming.scripting.jscript        back       


Validate Date data   
I'm trying to validate date data with Jscript. Here is what I have been 
using:

 // Check the date format(s) are good
paidThru = document.getElementsByName("PaidThrough");
for(i = 0; i < paidThru.length; i++)
{
    datenum = Date.parse(paidThru(i).value);
    if(isNaN(datenum))
    {
        alert("Date(s) must be in the format MM/DD/YYYY!");
        return;
    }
}

The problem is, when I input e.g. "6/31/2008", which is NOT a valid date, 
Date.parse returns a valid integer (1214895600000).
date: Mon, 16 Jun 2008 16:52:19 -0700   author:   Ron Hinds

Re: Validate Date data   
Ron Hinds  wrote:
> The problem is, when I input e.g. "6/31/2008", which is NOT a valid
> date, Date.parse returns a valid integer (1214895600000).

From ECMA-262 (JavaScript standard):

15.9.4.2 ... in general, the value produced by Date.parse is 
implementation-dependent when given any string value that could not be 
produced in that implementation by the toString or toUTCString method.


In other words, the behavior of Date.parse is undefined when given an 
invalid string. I suspect that in IE's implementation it "fixes up" the 
date the same way Date constructor and Date.setDay et al do (try 
executing    alert(new Date(2008,5,31).toString())    in your browser; 5 
stands for June since months are 0-based in Date's constructor).
-- 
With best wishes,
    Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not 
necessarily a good idea. It is hard to be sure where they are going to 
land, and it could be dangerous sitting under them as they fly 
overhead. -- RFC 1925
date: Mon, 16 Jun 2008 20:26:20 -0400   author:   Igor Tandetnik

Re: Validate Date data   
"Igor Tandetnik"  wrote in message 
news:eDxYFDB0IHA.4912@TK2MSFTNGP03.phx.gbl...
> Ron Hinds  wrote:
>> The problem is, when I input e.g. "6/31/2008", which is NOT a valid
>> date, Date.parse returns a valid integer (1214895600000).
>
> From ECMA-262 (JavaScript standard):
>
> 15.9.4.2 ... in general, the value produced by Date.parse is 
> implementation-dependent when given any string value that could not be 
> produced in that implementation by the toString or toUTCString method.
>
>
> In other words, the behavior of Date.parse is undefined when given an 
> invalid string. I suspect that in IE's implementation it "fixes up" the 
> date the same way Date constructor and Date.setDay et al do (try executing 
> alert(new Date(2008,5,31).toString())    in your browser; 5 stands for 
> June since months are 0-based in Date's constructor).

That's interesting - it comes back with July 1, 2008 - which is what I 
suspected. But, it doesn't solve my problem - i.e, how do I make sure a date 
is valid in JScript?
date: Tue, 17 Jun 2008 11:21:43 -0700   author:   Ron Hinds

Re: Validate Date data   
Ron Hinds  wrote:
> But, it doesn't solve my problem - i.e, how do I make sure
> a date is valid in JScript?

Use a regular expression to verify the format and extract individual 
fields. Then construct a Date object from individual year, month and day 
components. Finally, read the components back from Date object and check 
that they match what you passed in - this protects against "fix-up" 
behavior.
-- 
With best wishes,
    Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not 
necessarily a good idea. It is hard to be sure where they are going to 
land, and it could be dangerous sitting under them as they fly 
overhead. -- RFC 1925
date: Tue, 17 Jun 2008 14:34:54 -0400   author:   Igor Tandetnik

Re: Validate Date data   
"Ron Hinds"  wrote in message 
news:uaQ5$bK0IHA.4848@TK2MSFTNGP05.phx.gbl...
> "Igor Tandetnik"  wrote in message 
> news:eDxYFDB0IHA.4912@TK2MSFTNGP03.phx.gbl...
>> Ron Hinds  wrote:
>>> The problem is, when I input e.g. "6/31/2008", which is NOT a valid
>>> date, Date.parse returns a valid integer (1214895600000).
>>
>> From ECMA-262 (JavaScript standard):
>>
>> 15.9.4.2 ... in general, the value produced by Date.parse is 
>> implementation-dependent when given any string value that could not be 
>> produced in that implementation by the toString or toUTCString method.
>>
>>
>> In other words, the behavior of Date.parse is undefined when given an 
>> invalid string. I suspect that in IE's implementation it "fixes up" the 
>> date the same way Date constructor and Date.setDay et al do (try 
>> executing alert(new Date(2008,5,31).toString())    in your browser; 5 
>> stands for June since months are 0-based in Date's constructor).
>
> That's interesting - it comes back with July 1, 2008 - which is what I 
> suspected. But, it doesn't solve my problem - i.e, how do I make sure a 
> date is valid in JScript?
>
Here's one I use:

function isValidDate(year, month, day)
{
  try
  {
    var dt = new Date(year, month, day);
    if (isNaN(dt))
    {
      return false;
    }
    var iYear2 = dt.getFullYear();
    var iMonth2 = dt.getMonth();
    var iDate2 = dt.getDate();
    if (iYear2 != year ||
    iMonth2 != month ||
    iDate2 != day)
    {
      return false;
    }
  }
  catch(e)
  {
    return false;
  }
  return true;
}

I first split the actual string, usually on the / character and pass the 
resultant three parts to the function after taking 1 from the month.
If there aren't three parts it fails.
var iParts = dateString.split("/");
if i{Parts.length != 3) return false;
return isValidDate(iPart[2], iPart[1] - 1, iPart[0]);

Wrapped in a try/catch that returns false on an error.

-- 

Joe Fawcett (MVP - XML)

http://joe.fawcett.name
date: Wed, 18 Jun 2008 09:32:16 +0100   author:   Joe Fawcett am

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us