HighDots Forums  

Re: Date Validation

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Date Validation in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Date Validation - 05-11-2004 , 09:02 AM






JRS: In article <1084246273.468034@muldoon>, seen in
news:comp.lang.javascript, Steve Wright <wright (AT) wcc (DOT) govt.nz> posted at
Tue, 11 May 2004 15:31:13 :

Quote:
I am trying to validate if an entry on a form is a date.
I have adapted code I found here
http://www.codingforums.com/archive/index.php/t-14325 as below but I can't
seem the get the results that I am expecting.
First, therefore, we have to guess what results you are expecting.

Quote:
function isDate(sDate) {
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
if (re.test(sDate)) {
var dArr = sDate.split("/");
var d = new Date(sDate);
return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] &&
d.getFullYear() == dArr[2];
alert("A Date")
}
else {
alert("Not a Date")
return false;
}
}

If you had sought information in the FAQ of this newsgroup, rather than
by a Web search, all should have been well.

Code should be indented to show the intended, or actual (ideally, both)
structure.

A return statement returns; it does not just, as Delphi's Result, set a
result for later use. Therefore, that alert("A Date") cannot be
executed.

The second alert should read "Wrong pattern"; you may need a different
alert for a string such as "22/22/2222".

It is better to require \d\d rather than \d{1,2}; laxity encourages
slovenly thinking.

You yourself might write Christmas as 25/12/2004. But javascript is of
alien origin, and expects 12/25/2004 for that. So does the code, as can
be seen by inspecting use of DArr.

It is better still, if your data-enterers are intelligent, to ask for an
ISO 8601 style date, which no-one can mis-understand.


That code, laid out for readability and for not being wrapped by a
newsreader (as yours might have been), becomes

function isDate(sDate) {
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
if (re.test(sDate)) {
var dArr = sDate.split("/");
var d = new Date(sDate);
return d.getMonth() + 1 == dArr[0] &&
d.getDate() == dArr[1] && d.getFullYear() == dArr[2];
alert("A Date")
}
else {
alert("Not a Date")
return false;
}
}


That code fails for years before A.D. 100 <G>.

One should not (usually) just check whether a string is a valid date;
one should test whether it will be validly interpreted. Most Americans,
faced with "25/12/2004", will think "Christmas, but foreign"; but
Javascript believes that to mean 2006 January 12th. However, faced with
11/05/2004, they will think of Nov 5th rather than May 11th (it's still
late Autumn, though); and so will javascript.

It seems better to use the tests to set a variable OK, and end up by
returning that; use if (!OK) { /grumble/ }

If that's not enough, please re-ask listing the strings given, the
results expected, and the results obtained.

See <URL:http://www.merlyn.demon.co.uk/js-date4.htm#DVal>

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #2  
Old   
Randy Webb
 
Posts: n/a

Default Re: Date Validation - 05-11-2004 , 11:35 PM






Dr John Stockton wrote:

<snip>

Quote:
It is better still, if your data-enterers are intelligent, to ask for an
ISO 8601 style date, which no-one can mis-understand.

Is that to imply that users that use, and understand, a non-ISO 8601
style date are "un-intelligent"? I hope you are not that naive and
mis-guided by your apparent hatred of the US.

To ensure a date is "valid" is very simple, and employed *very* widely.
Even though you, and I alike, can type the date quicker, the problems
with date validation is very apparent by the number of sites that use
select lists (labeled for day, month, and year) on the web.

So, if I have three select lists, properly labeled, can you please tell
me how I can get an invalid date if the actual date is assembled on the
server? The only major problem associated with that approach is the
year, which a text input can be used for.


--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/


Reply With Quote
  #3  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Date Validation - 05-12-2004 , 12:24 PM



Randy Webb <hikksnotathome (AT) aol (DOT) com> writes:

Quote:
Dr John Stockton wrote:

snip

It is better still, if your data-enterers are intelligent, to ask
for an ISO 8601 style date, which no-one can mis-understand.

Is that to imply that users that use, and understand, a non-ISO 8601
style date are "un-intelligent"?
I can't read it that way, even if I try.

The logical conclusion from that statement must be that:

1) all intelligent users can understand ISO 8601 dates, and, since
intelligence wouldn't be mentioned otherwise, some non-intelligent
users might not understand ISO 8601. That is always a danger of using
non-local date formats.

2) Some users can misunderstand a non-ISO 8601 date (and probably even
some intelligent users).

So really, it's: All intelligent users can understand ISO 8601 dates.
No other combination is safe.

Quote:
So, if I have three select lists, properly labeled, can you please
tell me how I can get an invalid date if the actual date is assembled
on the server?
Using select elements doesn't prevent the 30th of February. You need
extra logic for that.

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #4  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Date Validation - 05-12-2004 , 12:56 PM



JRS: In article <xuadne_LJLovBjzd4p2dnA (AT) comcast (DOT) com>, seen in
news:comp.lang.javascript, Randy Webb <hikksnotathome (AT) aol (DOT) com> posted at
Tue, 11 May 2004 23:35:51 :
Quote:
Dr John Stockton wrote:

It is better still, if your data-enterers are intelligent, to ask for an
ISO 8601 style date, which no-one can mis-understand.


Is that to imply that users that use, and understand, a non-ISO 8601
style date are "un-intelligent"? I hope you are not that naive and
mis-guided by your apparent hatred of the US.
My objections are to stupidity; the US has not yet achieved a monopoly
of that. Indeed, it probably never will; but it shows no sign of losing
its majority, and therefore must expect to provide the best examples.

But you, who give no overt indication of location (but must be US, since
no-one else much dislikes a dislike of typical American habits), have
just provided a fine illustration of the problem; in saying that no-one
can misunderstand an ISO-style date, I in fact compliment (albeit
perhaps wrongly) Americans on their versatility. That implies only that
the non-intelligent, whoever they may be, *may* not be able to enter
ISO-style dates correctly.

It's a pity that the Americans did not choose - AIUI, they could well
have done so - to speak German; then, those learning English would have
been taught to do so professionally, and would at least have been able
to read it reliably. And, in that case, I'd also have been taught
German for more than a dozen lessons, which, as it has turned out, would
have been useful.

Anyone, of course, can mis-understand a US- (or UK-) style date; even an
intelligent American may well misunderstand a US date, if she believes
the date to have been written by a foreigner.

The answer to your question is therefore "No" to "to imply". If you had
been asking about that, it would have been "Yes" to "use" and "No" to
"understand", where "use" implies a free choice.


Quote:
To ensure a date is "valid" is very simple, and employed *very* widely.
Even though you, and I alike, can type the date quicker, the problems
with date validation is very apparent by the number of sites that use
select lists (labeled for day, month, and year) on the web.
That is probably because programmers like programming, and hence prefer
complex solutions - especially if paid for the amount of code, rather
than the quality of the result.

Quote:
So, if I have three select lists, properly labeled, can you please tell
me how I can get an invalid date if the actual date is assembled on the
server? The only major problem associated with that approach is the
year, which a text input can be used for.
You can get February 30th or 31st, or 29th in a common year, or Apr Jun
Sep Nov 30th - unless the date boxes are entered in Y M D order and the
D list length depends on the Y M selections; that is shown in my js-
date6.htm. The code there could perhaps be improved or shortened; but I
doubt whether it can be made shorter or simpler than a full validation
of a date string of specified format.




****


MAINLY TO STEVE :

Number(match[1]) etc. are fine; but +match[1] is sufficient; see
in the FAQ.

After checking by RegExp, there is no need to test all 3 of da my yr ;
without a RegExp check, I'm unsure whether all 3 need testing.

But a RegExp, or other capable check, should be used : consider
022/12/2004 - 22 Dec is a fine date, but there's no way of telling
whether the user started to enter a date such as 02/..., corrected
himself, and continued on the basis that 022 means 22, or whether he
meant 02/... and did not notice keyboard stutter.

For similar reasons, IMHO \d\d or \d{2} is safer than \d\d? or \d{1,2} .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.


Reply With Quote
  #5  
Old   
Randy Webb
 
Posts: n/a

Default Re: Date Validation - 05-12-2004 , 01:13 PM



Lasse Reichstein Nielsen wrote:



Quote:
So, if I have three select lists, properly labeled, can you please
tell me how I can get an invalid date if the actual date is assembled
on the server?


Using select elements doesn't prevent the 30th of February. You need
extra logic for that.
So its easier to determine a "valid date" than it is to determine the
days in February? I don't see it.


--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.