Date Comparison- error in code posted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anju07
    New Member
    • May 2007
    • 12

    Date Comparison- error in code posted

    hi,
    I am using the following javascript code to compare 2 dates, one of which is sysdate and the other is the date from my text field in my jsp.

    function rbvalidation(ct l)
    {
    var cal = new Array();
    cal.JAN = "January";
    cal.FEB = "February";
    cal.MAR = "March";
    cal.APR = "April";
    cal.MAY = "May";
    cal.JUN = "June";
    cal.JUL = "July";
    cal.AUG = "August";
    cal.SEP = "September" ;
    cal.OCT = "October";
    cal.NOV = "November";
    cal.DEC = "December";

    var sysDate = new Date();
    var effDateChar = ctl.value; // P_EFFECTIVE_DAT E

    // translate date entered by user into JS format
    var bufArray = effDateChar.spl it("-");
    var effDateMonth = cal[bufArray[1].toUpperCase()];
    var effDateDay = bufArray[0];
    var effDateYear = bufArray[2];
    var effDateDate = new Date(effDateMon th+" "+ effDateDay +
    ", "+effDateYe ar+" 23:59:59");

    // compare the 2 dates
    if (effDateDate < sysDate)
    {
    alert("The effective date must be today's date or a later date.");
    ctl.focus();
    return false;
    }
    return true;
    }
    </script>

    Im calling this as onChange=rbvali dation(this.val ue) in my text field....
    But, it is not working.Is something wrong with the code?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    To use associative arrays, try:
    Code:
    cal['JAN']="January";
    instead.

    Comment

    • anju07
      New Member
      • May 2007
      • 12

      #3
      Originally posted by acoder
      To use associative arrays, try:
      Code:
      cal['JAN']="January";
      instead.
      hi, i tried doing this, it yet does not work

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        To set the date, create a new Date object and then use setFullYear.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          There's no such thing as an 'associative array' in JavaScript. You'd be better off just creating a generic object:

          var cal = {
          JAN: 'January',
          FEB: 'February',
          and so on....
          };

          When you declare var cal = new Array(), and then you set cal.JAN, you're not actually modifying the Array 'value' of the object. Instead, you're adding a new property.

          This would do exactly the same thing:
          var cal = new Date();
          cal.JAN = 'January';

          Or even:
          var cal = Math;
          cal.JAN = 'January';

          Comment

          • anju07
            New Member
            • May 2007
            • 12

            #6
            Originally posted by pbmods
            There's no such thing as an 'associative array' in JavaScript. You'd be better off just creating a generic object:

            var cal = {
            JAN: 'January',
            FEB: 'February',
            and so on....
            };

            When you declare var cal = new Array(), and then you set cal.JAN, you're not actually modifying the Array 'value' of the object. Instead, you're adding a new property.

            This would do exactly the same thing:
            var cal = new Date();
            cal.JAN = 'January';

            Or even:
            var cal = Math;
            cal.JAN = 'January';

            I applied the above logic, but it yet does not validate the field

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Depending on the format of the date, you could use Date.parse().

              Code:
              if(Date.parse(ctl.value) < new Date()) {
              ...
              Date.parse() is a little finicky, though, so you might want to use a calendar control .

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by pbmods
                There's no such thing as an 'associative array' in JavaScript.
                True, but using it in that way does work. Whether it's a good idea/style is a different matter.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by anju07
                  I applied the above logic, but it yet does not validate the field
                  Have you used the setFullYear method? That should solve your problem. Currently, the method that you're using to define the date is not correct.

                  Have you considered using select dropdowns instead? It will be much easier to validate. With a text box, the user could enter anything.

                  Comment

                  Working...