validate a date using a regular expression

Discussion in 'Javascript' started by samuelberthelot@googlemail.com, Jun 12, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. reflex Guest

    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).
    reflex, Jun 12, 2007
    #2
    1. Advertising

  3. Guest

    thanks you ! it works.
    , Jun 12, 2007
    #3
  4. In comp.lang.javascript message <
    oglegroups.com>, Tue, 12 Jun 2007 10:11:46,
    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.
    Dr J R Stockton, Jun 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bogdan Zamfir

    Help on Regular experession to validate date

    Bogdan Zamfir, Aug 18, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    Hans Kesting
    Aug 19, 2003
  2. VSK
    Replies:
    2
    Views:
    2,282
  3. Ori
    Replies:
    2
    Views:
    21,272
    Brian W
    Jan 26, 2004
  4. Chris Kennedy

    Regular Expression to validate file extension

    Chris Kennedy, Jun 18, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    61,022
    colin.wiseman
    Sep 15, 2010
  5. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,244
    Michael Borgwardt
    May 30, 2004
Loading...

Share This Page