Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > validate a date using a regular expression

Reply
Thread Tools

validate a date using a regular expression

 
 
samuelberthelot@googlemail.com
Guest
Posts: n/a
 
      06-12-2007
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

 
Reply With Quote
 
 
 
 
reflex
Guest
Posts: n/a
 
      06-12-2007

(E-Mail 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).

 
Reply With Quote
 
 
 
 
samuelberthelot@googlemail.com
Guest
Posts: n/a
 
      06-12-2007
thanks you ! it works.

 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-12-2007
In comp.lang.javascript message <(E-Mail Removed)
oglegroups.com>, Tue, 12 Jun 2007 10:11:46,
http://www.velocityreviews.com/forums/(E-Mail 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.

--
(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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
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
Regular Expression to validate file extension Chris Kennedy ASP .Net 8 09-15-2010 01:20 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Validate US Phone number with Regular Expression Ori ASP .Net 2 01-26-2004 07:38 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM
Help on Regular experession to validate date Bogdan Zamfir ASP .Net 1 08-19-2003 07:51 AM



Advertisments