Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Best (and easiest) way to test that a "date string" (mm/dd/yyyy) is valid

Reply
Thread Tools

Best (and easiest) way to test that a "date string" (mm/dd/yyyy) is valid

 
 
Petyr David
Guest
Posts: n/a
 
      12-03-2006
Just looking for the simplest. right now my perl script returns an
error messge to the user if the date string is invalid. would like to
do this before accessing the server.

TX

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      12-03-2006

Petyr David wrote:
> Just looking for the simplest. right now my perl script returns an
> error messge to the user if the date string is invalid. would like to
> do this before accessing the server.


How do you get the date string from the user? Does she has to type it
in manually in strict mm/dd/yyyy format in a form's text field?

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      12-03-2006
Petyr David wrote:
> Just looking for the simplest. right now my perl script returns an
> error messge to the user if the date string is invalid. would like to
> do this before accessing the server.


If your date format is not flexible and you don't care to support ancient
browsers, a simple regular expression can quickly test that the input is
well-formed, but can't verify that the values are valid. For that you need
to split the values out and apply some logic.

If you want a solution that does all the work for you and can validate
arbitrary formats, check out my date lib at:
http://www.javascripttoolbox.com/lib/date

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-03-2006
In comp.lang.javascript message
<(E-Mail Removed) .com>, Sun, 3 Dec 2006
10:22:15, Petyr David <(E-Mail Removed)> wrote:
>Just looking for the simplest. right now my perl script returns an
>error messge to the user if the date string is invalid. would like to
>do this before accessing the server.


Always include the question within the article; the Subject is not
necessarily simultaneously visible.

It's a good idea to read the newsgroup and its FAQ. See below. You
could have discovered how to validate standard numeric date strings by
doing that, and then you should be able to adapt it for FFF strings.


Except that your "Best (and easiest) way" is rather meaningless. All
sensible methods are easy, if you only have to copy them. But does
"Best" refer to code size, code speed, or code comprehensibility, pr
proving code validity?


And you need to define what is valid. For today, 2006-12-3 is
unambiguous, but it is not valid by ISO 8601, which calls for
2006-12-03. You have only indicated that, for you, mm/dd/yyyy can be
good.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/> Old RC FAQ of 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
 
RobG
Guest
Posts: n/a
 
      12-04-2006
Petyr David wrote:
> Just looking for the simplest. right now my perl script returns an
> error messge to the user if the date string is invalid. would like to
> do this before accessing the server.


You need to define "valid" and "simplest". The simplest test I can
think of is for 3 numbers separated by "/":

function isValidDate(dateString){
return /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString);
}

But that may not be of much use - the input may not be a valid date,
e.g. 2/29/2006 will return true (as will a very large number of other
"valid" input strings).

If you want to check that the date is valid according to your required
mm/dd/yyyy, the easiest way is to firstly check the format, then that
it will generate a valid date:

function isValidDate(dateString){
if (/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString)){
var testDate = new Date(dateString);
var dateBits = dateString.split('/');
if ( dateBits[1] == testDate.getDate() &&
(dateBits[0]-1) == testDate.getMonth() ){
return true;
}
}
return false;
}

There are other methods that don't use a date object and may be
considered 'simpler' by some. Expecting mm/dd/yyyy to correctly
generate a date object when used with new Date() may be overly
dependent on browser implementations (the ECMAScript Language
specification doesn't say that it must, but browsers seem to).

Your required format is native to a small percentage of internet users.
You may wish to consider an international format (i.e. one that is not
peculiar to a small number of countries) such as the ISO format:
yyyy/mm/dd.

However, if your criteria for "simple" include implementation, you
might like to use a library such as that suggested by Matt K.

There is a lot of information on dates here:

<URL: http://www.merlyn.demon.co.uk/js-dates.htm >

You might also wish to search the archives, this question is asked
frequently.

<FAQENTRY>
Should there be an FAQ on date validation? It seems to be asked about
very frequently, a search on "validate date" returns 413 records.

There is a suggestion here from JRS that seems a reasonable start:

<URL:
http://groups.google.com.au/group/co...bd380cdf3407f9
>



--
Rob

 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-04-2006
In comp.lang.javascript message <(E-Mail Removed)>, Sun, 3
Dec 2006 16:17:26, Matt Kruse <(E-Mail Removed)> wrote:

>If your date format is not flexible and you don't care to support ancient
>browsers, a simple regular expression can quickly test that the input is
>well-formed, but can't verify that the values are valid. For that you need
>to split the values out and apply some logic.


An mm/dd/yyyy string will be read correctly by new Date(S), at least in
WinXP sp2 IE6 (showing that MS's idea of localisation is inadequate).

IF future standards were to implement a reverse method DObj.toNumStr
giving a format correspondingly localised, WHICH CERTAINLY THEY SHOULD,
ISTM that your statement would no longer be true.

OK = new Date(S).toNumStr() == S // untested, obviously.

It would be interesting to code .toNumStr() to make it give whichever of
mm/dd/yyyy and dd/mm/yyyy the current new Date(S) would use (as well as
..toNumStr(0) and .toNumStr(8601) giving yyyy-mm-dd and other arguments
giving other formats). Perhaps your library already does that; but I
don't yet see how it can be possible to do it without splitting the
values out.

function toNumStr(D) { var S
S =
LZ(D.getDate()) +'/'+ LZ(D.getMonth()+1) +'/'+ LZ(D.getFullYear())
return (+new Date(S) == +D) ? S :
S.replace(/(.{3})(.{3})/, "$2$1") }

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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
 
Petyr David
Guest
Posts: n/a
 
      12-04-2006
User types it in form's text field. This doesn't have to be anything
fancy. The people using this application are computer scientists and
researchers and should be able to follow directions. i simply want to
save then the time it would take for their query to run and return them
bogus results.

TX

On Dec 3, 2:53 pm, "VK" <(E-Mail Removed)> wrote:
> Petyr David wrote:
> > Just looking for the simplest. right now my perl script returns an
> > error messge to the user if the date string is invalid. would like to
> > do this before accessing the server.How do you get the date string from the user? Does she has to type it

> in manually in strict mm/dd/yyyy format in a form's text field?


 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-04-2006
Dr J R Stockton said the following on 12/4/2006 8:25 AM:
> In comp.lang.javascript message <(E-Mail Removed)>, Sun, 3
> Dec 2006 16:17:26, Matt Kruse <(E-Mail Removed)> wrote:
>
>> If your date format is not flexible and you don't care to support ancient
>> browsers, a simple regular expression can quickly test that the input is
>> well-formed, but can't verify that the values are valid. For that you need
>> to split the values out and apply some logic.

>
> An mm/dd/yyyy string will be read correctly by new Date(S), at least in
> WinXP sp2 IE6 (showing that MS's idea of localisation is inadequate).
>
> IF future standards were to implement a reverse method DObj.toNumStr
> giving a format correspondingly localised, WHICH CERTAINLY THEY SHOULD,
> ISTM that your statement would no longer be true.


And IF a frog had wings it wouldn't bump it's butt when it lands.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      12-04-2006
Petyr David wrote:

>Just looking for the simplest. right now my perl script returns an
>error messge to the user if the date string is invalid. would like to
>do this before accessing the server.



I'm not sure about the best...

You can use a regexp to extract the digit parts of a date. With those,
you can construct a new Date object. (Note that months start at index 0
for January.)

I found out that with the browsers I tested, this always succeeds, but
it is "corrected" to the proper calendar date, for example April 31 will
be turned into May 1.

So I could test if year, month, day are the same... but I'm guessing
that just testing the month is enough.

So here's a simple function:

function isValidDate(s) {
var m = s.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
if(!m) return false;
var d = new Date(m[3], m[1]-1, m[2]);
return m[1] == d.getMonth()+1;
}

HTH,
Bart.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-04-2006
In comp.lang.javascript message
<(E-Mail Removed). com>, Sun, 3 Dec 2006
17:19:13, RobG <(E-Mail Removed)> wrote:
>Petyr David wrote:
>> Just looking for the simplest. right now my perl script returns an
>> error messge to the user if the date string is invalid. would like to
>> do this before accessing the server.

>
>You need to define "valid" and "simplest". The simplest test I can
>think of is for 3 numbers separated by "/":
>
> function isValidDate(dateString){
> return /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString);
> }


Suggest isValidDatePattern or isValidDateFFFormat.

>But that may not be of much use - the input may not be a valid date,
>e.g. 2/29/2006 will return true (as will a very large number of other
>"valid" input strings).
>
>If you want to check that the date is valid according to your required
>mm/dd/yyyy, the easiest way is to firstly check the format, then that
>it will generate a valid date:
>
> function isValidDate(dateString){
> if (/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString)){


For the OP, ISTM d{2} not d{1,2}

> var testDate = new Date(dateString);


After reading the next comment, = new Date(part1, part2-1, part3)

> var dateBits = dateString.split('/');


If the RegExp is used with the match method, and parentheses are
inserted, the splitting will already have been done.

> if ( dateBits[1] == testDate.getDate() &&
> (dateBits[0]-1) == testDate.getMonth() ){


Given that the RegExp establishes that MM & DD are each in 00..99, ISTM
certain that one need only test the Month. If MM is impossible,
getMonth()+1 cannot give it; if DD is out of range, the apparent Month
will be changed by -1 or +1 / +2 / +3.

>There are other methods that don't use a date object and may be
>considered 'simpler' by some. Expecting mm/dd/yyyy to correctly
>generate a date object when used with new Date() may be overly
>dependent on browser implementations (the ECMAScript Language
>specification doesn't say that it must, but browsers seem to).


The optimistic will allow for the possibility that future browsers will
recognise MM/DD/YYYY or DD/MM/YYYY according to locale; and even for the
chance that, eventually, only the format chosen for the OS (mine is
YYYY-MM-DD) will be accepted. I cannot recall, and cannot now test,
whether IE4, which takes 01/02/03 as Jan 2, would take 21/02/03 as 21
Feb.


><FAQENTRY>
>Should there be an FAQ on date validation?


FAQ Section 3.2 refers. If it contained a Numbered List, I could cite
the number; but a search of the FAQ for any of the words date time
dates times lastModified will find it; the entry was some while ago
altered specifically to include the singular and plural forms, to aid
searching.

My Javascript Date and Time 4 : Validation is not a FAQ, since it puts
unasked questions on the same footing as asked ones.


--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
TEST TEST Test...Blah Blah Blah Generalbatguano@pacbell.net Computer Support 6 09-13-2006 01:53 AM
TEST TEST TEST Gazwad Computer Support 2 09-05-2003 07:32 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM
How is the best way to valid an entry ? Edmilson ASP .Net 1 06-30-2003 06:54 PM



Advertisments