0

I have a form that user should select start and end time of an event (only hour and minute). I want to validate the form in such a way that start time of course should be smaller than end time.

For this I have some select elements like this:

<select name="start_timeHour">
  <option value="00">00</option>
  <option value="01">01</option>
  <option value="02">02</option>
  <option value="03">03</option>
  <!-- UP TO 24 -->
</select>

<select name="start_timeMinutes">
.....
<select name="end_timeHour">
.....
<select name="end_timeMinutes">
<!-- I think you got the idea! -->

So I want to get the value of selected options for start and end (hours and minutes and compare them in an if statement). To start the things, I tried to get the value of start_timeHour but the alert function only shows undefined

<script type="text/javascript">
    function validateTime()
    {
        var startHour = 
            document.forms["add_new_schedule"]["start_timeHour"].value;
        alert("Start hour is ".startHour );
    }
</script>

Can you tell me what is the problem and how can I manage this?

Dumbo
  • 13,555
  • 54
  • 184
  • 288

2 Answers2

5

Your code is correct, your mistake is in

alert("Start hour is ".startHour );

You're getting startHour property from "Start hour is" String which is undefined.

alert("Start hour is "+startHour );
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
1

Try this

function validateTime()
{
    var startHour = document.getElementByName('start_timeHour');
    alert("Start hour is " + startHour.options[startHour.selectedIndex].value);
}
RaphaelDDL
  • 4,452
  • 2
  • 32
  • 56