By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Date validation and format help

P: n/a
Hi ,

Anyone have code or can help me write a function to test dates that are
:
1) valid dates
2) in the format dd/mm/yy

It only needs to work in IE6 and doesnt need to be modified for any
other date formats.
Thanks.

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

I created this function for my purposes:

//------------------------------------------------------------------------
function vdate(e2) {
var d,m,a;
var h = hoy();
var l = e2.length;
if (l==1) { d = "0" + e2; }
if (l==3) { d = e2.substr(0,2) ; m = "0" + e2.substr(2,1); }
if (l==4) { d = e2.substr(0,2) ; m = e2.substr(2,2); }
if (l==8) { d = e2.substr(0,2) ; m = e2.substr(2,2); a =
e2.substr(4,4); }
if (!d) d = e2.substr(0,2);
if (!m) m = h.substr(2,2);
if (!a) a = h.substr(4,4);
var b = (((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0)) ? 29 : 28;
var u = new Array(31,b,31,30,31,30,31,31,30,31,30,31);
if (d<1 || d>u[m-1]) { alert("Día erróneo. Máximo: "+u[m-1]); return
false; }
if (m<1 || m>12) { alert("Mes erróneo"); return false; }
c.value = d + "/" + m + "/" + a;
return c.value;
}

function hoy() {
var hoy = new Date();
var dia = ((hoy.getDate()<10) ? "0" : "") + hoy.getDate();
var mes = (((hoy.getMonth()+1)<10) ? "0" : "") + (hoy.getMonth()+1);
var any = hoy.getFullYear();
return dia+mes+any;
}
//------------------------------------------------------------------------
You can enter a date with only the day, day+month or date complete.

Regards,

Enzo
Jul 23 '05 #2

P: n/a
JRS: In article <11*********************@g44g2000cwa.googlegroups. com>,
dated Fri, 8 Jul 2005 04:36:46, seen in news:comp.lang.javascript,
cc******@ntu.ac.uk posted :
Anyone have code or can help me write a function to test dates that are
:
1) valid dates
2) in the format dd/mm/yy

It only needs to work in IE6 and doesnt need to be modified for any
other date formats.


Read the newsgroup FAQ with care; see signature below. Enzo's method is
unnecessarily long.

Use dd/mm/yyyy in preference to dd/mm/yy; but use if possible yyyy-mm-
dd, which is an international, EU, UK and (snigger) US Federal standard.

What's NTU ?

--
© 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.
Jul 23 '05 #3

P: n/a

Use a regexp, check against:

isValid=/^[1-31]{1,2}\/[1-12]{1,2}\/[0-5]{1,2}$/;
if (isValid.test(YOURSTRINGHERE)) {....}
Danny
On Fri, 08 Jul 2005 04:36:46 -0700, <cc******@ntu.ac.uk> wrote:
Hi ,

Anyone have code or can help me write a function to test dates that are
:
1) valid dates
2) in the format dd/mm/yy

It only needs to work in IE6 and doesnt need to be modified for any
other date formats.
Thanks.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #4

P: n/a
Lee
Danny said:


Use a regexp, check against:

isValid=/^[1-31]{1,2}\/[1-12]{1,2}\/[0-5]{1,2}$/;
if (isValid.test(YOURSTRINGHERE)) {....}


That's not how ranges work in regular expressions.
It's a range of individual characters, not numeric values.

The pattern "[1-31]" will match any one character from the list
("1", "2", "3", or "1"). The redundant "1", might be considered
an error, but probably not.

Jul 23 '05 #5

P: n/a
Lee wrote:
Danny said:

Use a regexp, check against:

isValid=/^[1-31]{1,2}\/[1-12]{1,2}\/[0-5]{1,2}$/;
if (isValid.test(YOURSTRINGHERE)) {....}

That's not how ranges work in regular expressions.
It's a range of individual characters, not numeric values.

The pattern "[1-31]" will match any one character from the list
("1", "2", "3", or "1"). The redundant "1", might be considered
an error, but probably not.


Not to mention that even if it worked as you suppose it was intended,
it is a totally inadequate 'validation' of a date. But perhaps we're
being harsh, the response appears to have been posted 18 hours before
the question was asked...
--
Rob
Jul 23 '05 #6

P: n/a
Danny wrote:

Use a regexp, check against:

isValid=/^[1-31]{1,2}\/[1-12]{1,2}\/[0-5]{1,2}$/;
if (isValid.test(YOURSTRINGHERE)) {....}


That's nonsense.
Mick

Jul 23 '05 #7

P: n/a
Thanks,
I usually do use date formats that cannot be interpreted incorrectly
like dd-mon-yyyy but for this user/application requirement I have to
let users enter dd/mm/yy. The app needs to test this before converting
to a full SQL2K format.
What's NTU ?

Notingham Trent University

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.