Validation script working on Firefox and Chrome, not working on IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hotice
    New Member
    • Apr 2010
    • 5

    Validation script working on Firefox and Chrome, not working on IE

    I have a form for registering some bookings. The problem is that I want to validate the data users enter in this form. All works fine, excepting the booking hours (ready by and closing time).

    This is the part of the script that does not work:

    [CODE=javascript]
    function validate_fields ()
    {

    if (document.getEl ementById("comp ania").value.le ngth == "0")
    {
    document.getEle mentById("compa nia").style.bac kground = "#FD9B80";
    submitOK="false ";
    }

    ...............

    var data = new String(document .getElementById ("data").value) ;



    var month = data[3]+data[4];
    var day = data[0]+data[1];
    var year = data[6]+data[7]+data[8]+data[9];

    var ready_h = document.getEle mentById("ready _h").value;
    var ready_m = document.getEle mentById("ready _m").value;
    var close_h = document.getEle mentById("close _h").value;
    var close_m = document.getEle mentById("close _m").value;

    var date_hour_ready = new Date(month + "/" + day + "/" + year + " " + ready_h + ":" + ready_m);
    var date_hour_close = new Date(month + "/" + day + "/" + year + " " + close_h + ":" + close_m);
    var last_booking_ho ur = new Date (month + "/" + day + "/" + year + " " + "19:00");
    var first_booking_h our = new Date (month + "/" + day + "/" + year + " " + "09:00");
    var current_date = new Date();

    if (isNaN(ready_h) )
    {
    document.getEle mentById("ready _h").style.back ground = "#FD9B80";
    submitOK="false ";
    }

    ...............

    if ((data_ora_clos e - data_ora_ready) < 2*60*60*1000)
    {
    document.getEle mentById("ready _h").style.back ground = "#FD9B80";
    document.getEle mentById("ready _m").style.back ground = "#FD9B80";
    document.getEle mentById("close _h").style.back ground = "#FD9B80";
    document.getEle mentById("close _m").style.back ground = "#FD9B80";
    alert("Pickup time should be at least 2 hours.");
    submitOK="false ";
    }


    if (data_ora_ready <= current_date)
    {
    document.getEle mentById("ready _h").style.back ground = "#FD9B80";
    document.getEle mentById("ready _m").style.back ground = "#FD9B80";
    //alert("Ready by < closing time");
    submitOK="false ";
    }

    if (data_ora_close > last_booking_ho ur)
    {
    document.getEle mentById("close _h").style.back ground = "#FD9B80";
    document.getEle mentById("close _m").style.back ground = "#FD9B80";
    alert("Last pickup time should be 19:00");
    submitOK="false ";
    }

    if (data_ora_ready < first_booking_h our)
    {
    document.getEle mentById("ready _h").style.back ground = "#FD9B80";
    document.getEle mentById("ready _m").style.back ground = "#FD9B80";
    alert("You cannot register bookings earlier than 09:00");
    submitOK="false ";
    }


    if (submitOK=="fal se")
    {
    return false;
    }

    }
    [/CODE]

    There are a lot of other ifs in that function, but just the last four are the problem. They work only on Firefox and Chrome.

    Do you have any idea why on Internet Explorer this does not work?
  • Hotice
    New Member
    • Apr 2010
    • 5

    #2
    I don't know if the problem could be here, but here is the HTML code too:

    Code:
    <script type="text/javascript" src="js/new_shipment_fields_validation.js"></script>
    
    <form action='submit.php' method='post' onsubmit='return validate_fields()'>
    ........
    <input type='text' name='ready_h' id='ready_h' size='1'>
    <input type='text' name='ready_m' id='ready_m' size='1'>
    <input type='text' name='close_h' id='close_h' size='1'>
    <input type='text' name='close_m' id='close_m' size='1'>
    .......
    
    <input type='submit' value='Register booking'>
    Cheers!

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      try to make the ids and names different and see whether that helps or not ... i think to remember that having both the same could cause problems with IE ...

      kind regards

      Comment

      • Hotice
        New Member
        • Apr 2010
        • 5

        #4
        Originally posted by gits
        try to make the ids and names different and see whether that helps or not ... i think to remember that having both the same could cause problems with IE ...

        kind regards
        This does not solve the problem (I have more than 40 elements in that form with the same name and ID. And the only validation that does not work is for booking hours).

        But I did the following thing: I added one more line:
        alert(date_hour _ready);

        Now, when I submit the form, on Firefox an alert message is displayed, with the following text: "Sun Apr 25 2010 14:07:00 GMT+0300 (GTB Daylight Time)". On Internet Explorer, the alert box says just "NaN". What could be the problem?

        Many thanks!

        Comment

        • Hotice
          New Member
          • Apr 2010
          • 5

          #5
          Problem solved.
          For selecting one character of a string, Internet Explorer does not recognize this format: string[2].
          So, I replaced
          Code:
          var day = data[0]+data[1];
          with
          Code:
          var day = data.charAt(0)+data.charAt(1);
          Many thanks to all for reading my post.

          Comment

          Working...