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

validate a date using a regular expression

P: n/a
Hello,
I would like to validate a date in a textbox on the onChange event.
The date must be in the format 01/01/2007

I would like to use a regular expression to validate it but I'm not
very familiar with them. How can I do it ?

Thanks

Jun 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

samuelberthe...@googlemail.com napsal:
Hello,
I would like to validate a date in a textbox on the onChange event.
The date must be in the format 01/01/2007

I would like to use a regular expression to validate it but I'm not
very familiar with them. How can I do it ?

Thanks
var regExp = /^\d{2}\/\d{2}\/\d{4}$/;
var date = "01/01/2007";

if(regExp.test(date)) {
alert('yep its correct date');
} else {
alert('this is not correct date');
}

This regexp will only test format not date. (correct date will be
99/99/9999).

Jun 12 '07 #2

P: n/a
thanks you ! it works.

Jun 12 '07 #3

P: n/a
In comp.lang.javascript message <11**********************@o11g2000prd.go
oglegroups.com>, Tue, 12 Jun 2007 10:11:46,
sa*************@googlemail.com posted:
>I would like to validate a date in a textbox on the onChange event.
The date must be in the format 01/01/2007
That's a really silly format. It could represent, in that order, 1st
Jan 2007 or Jan 1st 2007. Now consider the following day ... . Numeric
dates should be ISO 8601.
>I would like to use a regular expression to validate it but I'm not
very familiar with them. How can I do it ?
You can easily validate the pattern thus, but not *easily* the date
itself. However, validating the date itself after a pattern test is
trivial, once the principle is realised. See below.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.