input day in the past

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • avcc

    #1

    input day in the past

    Hello,

    If have a HTML form where a user can type a date, this date must be today,
    or in the future. To test it i made this javascript

    function validate()
    {
    var cdate_h = form.elements["cdate"].value;


    if (cdate_h.length == 0)
    {
    alert("completi on date");
    return;
    }

    calDate = new Date();

    var day = calDate.getDate ();
    var month = calDate.getMont h() + 1;
    var year = calDate.getFull Year();

    var today = year * 10000 + month * 100 + day;

    var pos1=cdate_h.in dexOf("-"); // date
    format YYY-MM-DD
    var pos2=cdate_h.in dexOf("-",pos1+1);
    var strYear=cdate_h .substring(0,po s1);
    var strMonth=cdate_ h.substring(pos 1+1,pos2);
    var strDay=cdate_h. substring(pos2+ 1);

    var userdate = strYear * 10000 + strMonth * 100 + strDay;

    if (parseFloat(use rdate) < parseFloat(toda y))
    {
    alert ("Completion date can not be in the past");
    return;
    }
    }

    When I debug the variables userdate and today it looks fine, but the if
    statement does not work WHY ??????

    Can someone help me ? Many thanks in advance.

    Richard



  • Lasse Reichstein Nielsen

    #2
    Re: input day in the past

    "avcc" <avcc@freeler.n l> writes:
    [color=blue]
    > var userdate = strYear * 10000 + strMonth * 100 + strDay;[/color]

    In this, strDay is a string, so it as appended to userdate to form a
    new string.
    If strYear is "2003", strMonth is "10" and strDay is "18", then the result
    is
    (20030000 + 1000 + "18") = "2003100018 "
    It is not the expected 20031018.

    If you change the above line to
    var userdate = strYear * 10000 + strMonth * 100 + (+strDay);
    of
    var userdate = strYear * 10000 + strMonth * 100 + strDay * 1;
    then userdate becomes a number, and the one you expect.
    [color=blue]
    > if (parseFloat(use rdate) < parseFloat(toda y))[/color]

    You can drop parseFloat on today, it is already a number. If you make
    the fix above, so is userdate.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Cornford

      #3
      Re: input day in the past

      "avcc" <avcc@freeler.n l> wrote in message
      news:Nn8kb.3092 93$4w.35020532@ amsnews03.chell o.com...[color=blue]
      >If have a HTML form where a user can type a date, this date
      >must be today, or in the future. To test it i made this javascript[/color]

      Generally:-
      <URL: http://www.merlyn.demon.co.uk/js-dates.htm >
      [color=blue]
      > function validate()
      > {
      > var cdate_h = form.elements["cdate"].value;
      > if (cdate_h.length == 0)
      > {
      > alert("completi on date");
      > return;
      > }
      > calDate = new Date();
      >
      > var day = calDate.getDate ();
      > var month = calDate.getMont h() + 1;
      > var year = calDate.getFull Year();
      > var today = year * 10000 + month * 100 + day;
      >
      > var pos1=cdate_h.in dexOf("-"); // date format YYY-MM-DD
      > var pos2=cdate_h.in dexOf("-",pos1+1);
      > var strYear=cdate_h .substring(0,po s1);
      > var strMonth=cdate_ h.substring(pos 1+1,pos2);
      > var strDay=cdate_h. substring(pos2+ 1);[/color]

      Specifically, the values of strMonth, strYear and strDay are strings. In
      the next line:-
      [color=blue]
      > var userdate = strYear * 10000 + strMonth * 100 + strDay;[/color]

      - the use of the multiplication operator will automatically type-convert
      strYear and strMonth to numbers but the + operator is a duel purpose
      addition and string concatenation operator and if either of its operands
      is a string it does concatenation. strDay is a string so even if
      ((strYear*10000 )+(strMonth*100 )) produced a numeric value the final +
      operator will have that value converted into a string and concatenate
      strDay to it. Later using parsFloat (on an integer?) will have that
      string converted back to a number for the comparison but that string is
      one to two characters longer than you are expecting thus the number is
      at least 10 to 100 times larger.
      [color=blue]
      > if (parseFloat(use rdate) < parseFloat(toda y))
      > {
      > alert ("Completion date can not be in the past");
      > return;
      > }
      > }[/color]

      Richard.


      Comment

      • Dr John Stockton

        #4
        Re: input day in the past

        JRS: In article <Nn8kb.309293$4 w.35020532@amsn ews03.chello.co m>, seen
        in news:comp.lang. javascript, avcc <avcc@freeler.n l> posted at Sat, 18
        Oct 2003 10:14:05 :-
        [color=blue]
        >If have a HTML form where a user can type a date, this date must be today,
        >or in the future. To test it i made this javascript
        >
        >function validate()
        >{
        > var cdate_h = form.elements["cdate"].value;
        >
        >
        > if (cdate_h.length == 0)
        > {
        > alert("completi on date");
        > return;
        > }
        >
        > calDate = new Date();
        >
        > var day = calDate.getDate ();
        > var month = calDate.getMont h() + 1;
        > var year = calDate.getFull Year();
        >
        > var today = year * 10000 + month * 100 + day;
        >
        > var pos1=cdate_h.in dexOf("-"); // date
        >format YYY-MM-DD
        > var pos2=cdate_h.in dexOf("-",pos1+1);
        > var strYear=cdate_h .substring(0,po s1);
        > var strMonth=cdate_ h.substring(pos 1+1,pos2);
        > var strDay=cdate_h. substring(pos2+ 1);
        >
        > var userdate = strYear * 10000 + strMonth * 100 + strDay;
        >
        > if (parseFloat(use rdate) < parseFloat(toda y))
        > {
        > alert ("Completion date can not be in the past");
        > return;
        > }
        >}
        >
        >When I debug the variables userdate and today it looks fine, but the if
        >statement does not work WHY ??????
        >
        >Can someone help me ? Many thanks in advance.[/color]


        The last thing that is wrong is that you did not tell us explicitly how
        it did not work. Perhaps it works for some combinations of dates.

        A further error is that, in English, you should be writing "cannot"
        instead of "can not". The words "can not" really mean "possibly not",
        whereas "cannot" means "certainly not". This is a European secret which
        the Americans have not yet discovered.

        The previous thing is that, after finding (I suppose) that the alert did
        or did not occur, you should immediately have preceded it with, for
        example, alert(userdate+ '\n'+today) , in order to see what had
        happened.

        The explanation of what happens has already been given.

        The code is too long. AIUI, cdate_h is a string in YMD format.
        Assuming it has a 4-digit year, consider

        S = "2003-01-05"
        if ( new Date() - new Date(S.replace(/-/g, '/')) > 0 )
        alert("Completi on date cannot be in the past")

        If it may be a 2-digit year,
        S = S.replace(/(\d+)\D+(\d+)\D +(\d+)/, '$2/$3/$1')
        will generate a DDF M/D/Y which I believe all Javascript will
        understand.

        But if it will be a 2-digit year, then, for a while, use
        S = '20'+S.replace(/-/g, '/')

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...