By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

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

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. function validate_fields()
  2. {
  3.  
  4. if (document.getElementById("compania").value.length == "0")
  5.     {
  6.     document.getElementById("compania").style.background = "#FD9B80";
  7.     submitOK="false";
  8.     }
  9.  
  10. ...............
  11.  
  12. var data = new String(document.getElementById("data").value);
  13.  
  14.  
  15.  
  16. var month = data[3]+data[4];
  17. var day = data[0]+data[1];
  18. var year = data[6]+data[7]+data[8]+data[9];
  19.  
  20. var ready_h = document.getElementById("ready_h").value;
  21. var ready_m = document.getElementById("ready_m").value;
  22. var close_h = document.getElementById("close_h").value;
  23. var close_m = document.getElementById("close_m").value;
  24.  
  25. var date_hour_ready = new Date(month + "/" + day + "/" + year + " " + ready_h + ":" + ready_m);
  26. var date_hour_close = new Date(month + "/" + day + "/" + year + " " + close_h + ":" + close_m);
  27. var last_booking_hour = new Date (month + "/" + day + "/" + year + " " + "19:00");
  28. var first_booking_hour = new Date (month + "/" + day + "/" + year + " " + "09:00");
  29. var current_date = new Date();
  30.  
  31. if (isNaN(ready_h))
  32.     {
  33.     document.getElementById("ready_h").style.background = "#FD9B80";
  34.     submitOK="false";
  35.     }
  36.  
  37. ...............
  38.  
  39. if ((data_ora_close - data_ora_ready) < 2*60*60*1000)
  40.     {
  41.     document.getElementById("ready_h").style.background = "#FD9B80";
  42.     document.getElementById("ready_m").style.background = "#FD9B80";
  43.     document.getElementById("close_h").style.background = "#FD9B80";
  44.     document.getElementById("close_m").style.background = "#FD9B80";
  45.     alert("Pickup time should be at least 2 hours.");
  46.     submitOK="false";
  47.     }
  48.  
  49.  
  50. if (data_ora_ready <= current_date)
  51.     {
  52.     document.getElementById("ready_h").style.background = "#FD9B80";
  53.     document.getElementById("ready_m").style.background = "#FD9B80";
  54.     //alert("Ready by < closing time");
  55.     submitOK="false";
  56.     }
  57.  
  58. if (data_ora_close > last_booking_hour)
  59.     {
  60.     document.getElementById("close_h").style.background = "#FD9B80";
  61.     document.getElementById("close_m").style.background = "#FD9B80";
  62.     alert("Last pickup time should be 19:00");
  63.     submitOK="false";
  64.     }
  65.  
  66. if (data_ora_ready < first_booking_hour)
  67.     {
  68.     document.getElementById("ready_h").style.background = "#FD9B80";
  69.     document.getElementById("ready_m").style.background = "#FD9B80";
  70.     alert("You cannot register bookings earlier than 09:00");
  71.     submitOK="false";
  72.     }
  73.  
  74.  
  75. if (submitOK=="false")
  76.  {
  77.  return false;
  78.  }
  79.  
  80. }
  81.  
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?
Apr 23 '10 #1
Share this Question
Share on Google+
4 Replies


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

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="js/new_shipment_fields_validation.js"></script>
  2.  
  3. <form action='submit.php' method='post' onsubmit='return validate_fields()'>
  4. ........
  5. <input type='text' name='ready_h' id='ready_h' size='1'>
  6. <input type='text' name='ready_m' id='ready_m' size='1'>
  7. <input type='text' name='close_h' id='close_h' size='1'>
  8. <input type='text' name='close_m' id='close_m' size='1'>
  9. .......
  10.  
  11. <input type='submit' value='Register booking'>
  12.  
Cheers!
Apr 23 '10 #2

gits
Expert Mod 5K+
P: 5,377
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
Apr 23 '10 #3

P: 5
@gits
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!
Apr 25 '10 #4

P: 5
Problem solved.
For selecting one character of a string, Internet Explorer does not recognize this format: string[2].
So, I replaced
Expand|Select|Wrap|Line Numbers
  1. var day = data[0]+data[1];
with
Expand|Select|Wrap|Line Numbers
  1. var day = data.charAt(0)+data.charAt(1);
Many thanks to all for reading my post.
Apr 25 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.