#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    52
    Rep Power
    13

    JavaScript thinks 1 > 10 ??


    I have a control function like this
    function control()
    {
    if(document.getElementById('valid_fromday').value==document.getElementById('valid_today').value && document.getElementById('valid_frommo').value==document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value==document.getElementById('valid_toyear').value)
    {
    alert('ERROR! beginning and ending dates of a period must be different');
    return false;
    }
    else if(document.getElementById('valid_fromday').value>= document.getElementById('valid_today').value && document.getElementById('valid_frommo').value>= document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value>= document.getElementById('valid_toyear').value)
    {
    alert('ERROR! beginning date of the period can not be later then ending date');
    return false;
    } ...

    the select lists are created using PHP like this
    //DATE ARRAYS
    $day=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
    $mo=array(0,Jan,Feb,Mar,Apr,Mai,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
    $thisyear=date("Y");
    for($i=($thisyear-2);$i<($thisyear+5);$i++)
    {
    $year[]=$i;
    }
    ...
    From <select size="1" name="valid_fromday">
    <?
    for($i=1;$i<(count($day));$i++)
    {
    echo("<option value=\"$i\">$day[$i]</option>");
    }
    ?>
    </select> .... month year ext.

    When the days or months between 1-9 selected as from date and
    days and months greater 10 as to date, function alerts first date is later then second.
    And when dates in the same month selected then it alerts dates must be different.
    Why? and how can I solve the problem?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    Too tired, too much code to read so I'll have a guess. The values you are getting from the form inputs are strings. Use:

    parseInt(document.getElement....)

    to convert them to integers, then compare them.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    52
    Rep Power
    13

    thanks binky


    but it doesn't work.
    I have tried it with parseInt as you suggested, the control function couldn't detect the misentered dates. (The script has run errorless)

    When I create the select lists with html coding, the control function works as expected. But when I create them using array in PHP as written in my first message they cause this problem. I am thinking it is not a PHP problem because I can let the date data store in db correctly and problem free.

    Any help?
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    Right then, more awake today. Firstly...

    if(document.getElementById('valid_fromday').value>= document.getElementById('valid_today').value && document.getElementById('valid_frommo').value>= document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value>= document.getElementById('valid_toyear').value)
    {
    alert('ERROR! beginning date of the period can not be later then ending date');
    return false;
    }

    ...this bit isn't correct. Not all of these have to be true to make the 'from' date later than the 'to' date. The day, month and year don't all have to be greater. If the day is less but the month is greater and the year the same, this statement wont return true.

    What I would suggest is :

    var fromDate = new Date(fromYear,fromMonth,fromDay);
    var toDate = new Date(toYear,toMonth,toDay);
    if (fromDate.getTime() > toDate.getTime()) {
    alert('ERROR! beginning date of the period can not be later then ending date');
    return false;
    }

    That would do a better comparison.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    52
    Rep Power
    13

    binky I need further help


    binky,
    I have asked about my problem also in PHP forum, and member merkinmuffley has replied as follows;
    teknoart,

    to get the value of the selected option from the select box, dont you have to go further than getting the .value attribute of the select box itself. the select box's value is not the same as the value of the selected option, because each option is also an object. they're stored in the .options array, and you can get the selected one by the .selectedIndex attribute...

    mySelect.options[mySelect.selectedIndex].value

    also I think you can shorten that to:

    mySelect[mySelect.selectedIndex].value

    but don't quote me on that one. anyway the point is that you might have to rewrite the conditionals to something like this:

    var fromDay = document.getElementById('valid_fromday');
    var toDay = document.getElementById('valid_today');

    if (fromDay.options[fromDay.selectedIndex].value == toDay.options[toDay.selectedIndex].value && etc etc)

    hth

    /nick
    ----------------------
    It was true, and what you suggest was also true, thatwhy I have modified my control function like this;
    function control()
    {
    var fromDay = document.getElementById('valid_fromday');
    var toDay = document.getElementById('valid_today');
    var fromMonth = document.getElementById('valid_frommo');
    var toMonth = document.getElementById('valid_tomo');
    var fromYear = document.getElementById('valid_fromyear');
    var toYear = document.getElementById('valid_toyear');
    var fromDay=fromDay.options[fromDay.selectedIndex].value;
    var toDay=toDay.options[toDay.selectedIndex].value;
    var fromMonth=fromMonth.options[fromMonth.selectedIndex].value;
    var toMonth=toMonth.options[toMonth.selectedIndex].value;
    var fromYear=fromYear.options[fromYear.selectedIndex].value;
    var toYear=toYear.options[toYear.selectedIndex].value;
    var fromDate = new Date(fromYear,fromMonth,fromDay);
    var toDate = new Date(toYear,toMonth,toDay);
    if (fromDate.getTime() == toDate.getTime())
    {
    alert('ERROR! beginning and ending dates of a period must be different');
    return false;
    }
    else if (fromDate.getTime() > toDate.getTime())
    {
    alert('ERROR! beginning date of the period can not be later then ending date');
    return false;
    }...
    well done except year comparisions. Please help me further. Thanks a lot for your support

IMN logo majestic logo threadwatch logo seochat tools logo