RegExp for Date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zzzxtreme@yahoo.com

    RegExp for Date

    hello i have this function to check date (not mine)


    function (s_date) {
    // check format
    if (!re_dt.test(s_ date))
    return false;
    // check allowed ranges
    if (RegExp.$1 > 31 || RegExp.$2 > 12)
    return false;
    // check number of day in month
    var dt_test = new Date(RegExp.$3, Number(RegExp.$ 2-1), RegExp.$1);
    if (dt_test.getMon th() != Number(RegExp.$ 2-1))
    return false;
    return true;
    }


    it will check in this format DD-MM-YYYY
    is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
    function?

  • RobG

    #2
    Re: RegExp for Date

    zzzxtreme@yahoo .com wrote:[color=blue]
    > hello i have this function to check date (not mine)
    >
    >
    > function (s_date) {
    > // check format
    > if (!re_dt.test(s_ date))
    > return false;
    > // check allowed ranges
    > if (RegExp.$1 > 31 || RegExp.$2 > 12)
    > return false;
    > // check number of day in month
    > var dt_test = new Date(RegExp.$3, Number(RegExp.$ 2-1), RegExp.$1);
    > if (dt_test.getMon th() != Number(RegExp.$ 2-1))
    > return false;
    > return true;
    > }
    >
    >
    > it will check in this format DD-MM-YYYY
    > is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
    > function?
    >[/color]

    How are we to know, you haven't shown what the actual regular exression
    is. Presumably there is something somwhere that gives 're_dt' a value?




    --
    RobG

    Comment

    • Dr John Stockton

      #3
      Re: RegExp for Date

      JRS: In article <1117189072.863 709.15490@o13g2 000cwo.googlegr oups.com>,
      dated Fri, 27 May 2005 03:17:52, seen in news:comp.lang. javascript,
      zzzxtreme@yahoo .com posted :[color=blue]
      >hello i have this function to check date (not mine)
      >
      >
      >function (s_date) {
      > // check format
      > if (!re_dt.test(s_ date))
      > return false;
      > // check allowed ranges
      > if (RegExp.$1 > 31 || RegExp.$2 > 12)
      > return false;
      > // check number of day in month
      > var dt_test = new Date(RegExp.$3, Number(RegExp.$ 2-1),
      >RegExp.$1);
      > if (dt_test.getMon th() != Number(RegExp.$ 2-1))
      > return false;
      > return true;
      > }
      >
      >
      >it will check in this format DD-MM-YYYY
      >is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
      >function?[/color]

      Yes. But neither DMY not MDY is good; YMD is how dates should be,
      especially in data.

      You should NOT in one input allow either DMY or MDY to be valid. One
      can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
      apart - but is 01-07-2006, which is certainly valid, in Winter or in
      Summer?

      Therefore, your proposed modification would be too unwise to support.


      There is no point in checking > 31 and > 12 as such; and you would, if
      there were, also need to consider <=0.

      The first 'return false' is probably unnecessary; if the final check is
      judicious, it will catch soon enough what the first one would. But it
      could be made to give a message "that does not even look like a date".

      In js-date4.htm#DVal :-


      function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
      with (new Date(y, m, d))
      return (getMonth()==m && getDate()==d) /* was y, m */ }


      Read the newsgroup FAQ, especially sections 2.3 and 3.2.p (& 4.30).
      <FAQENTRY> Could Sec 3.2 become an <ol type=a> list, please </FAQENTRY>
      See below.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • RobG

        #4
        Re: RegExp for Date

        Dr John Stockton wrote:
        [...][color=blue]
        > You should NOT in one input allow either DMY or MDY to be valid. One
        > can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
        > apart - but is 01-07-2006, which is certainly valid, in Winter or in
        > Summer?[/color]

        For me at least, 01-07-2006 will be winter (global warming ignored).
        It will also likely be winter in that place that persists with M-D-Y
        format, but 6 months earlier. :-)

        Of course those who share the same hemisphere will disagree.

        [...]

        --
        Rob

        Comment

        Working...