JRS: In article <11*********************@o13g2000cwo.googlegroups. com>,
dated Fri, 27 May 2005 03:17:52, seen in news:comp.lang.javascript,
zz*******@yahoo.com posted :
hello i have this function to check date (not mine)
function (s_date) {
// check format
if (!re_dt.test(s_date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1),
RegExp.$1);
if (dt_test.getMonth() != Number(RegExp.$2-1))
return false;
return true;
}
it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?
Yes. But neither DMY not MDY is good; YMD is how dates should be,
especially in data.
You should NOT in one input allow either DMY or MDY to be valid. One
can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
apart - but is 01-07-2006, which is certainly valid, in Winter or in
Summer?
Therefore, your proposed modification would be too unwise to support.
There is no point in checking > 31 and > 12 as such; and you would, if
there were, also need to consider <=0.
The first 'return false' is probably unnecessary; if the final check is
judicious, it will catch soon enough what the first one would. But it
could be made to give a message "that does not even look like a date".
In js-date4.htm#DVal :-
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return (getMonth()==m && getDate()==d) /* was y, m */ }
Read the newsgroup FAQ, especially sections 2.3 and 3.2.p (& 4.30).
<FAQENTRY> Could Sec 3.2 become an <ol type=a> list, please </FAQENTRY>
See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/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.