validate a date using a regular expression

S

samuelberthelot

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
 
R

reflex

(e-mail address removed) 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).
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Tue, 12 Jun 2007 10:11:46,
(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top