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

Start Time and End Time Validation

P: 10
I have select boxes for starttime and endtime in hours, minutes and AM/PM.
I need to validate the time so that end time should be greater than start time.
The AM and PM should also be validated.

Any help appreciated !

below is my code
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2.  <HEAD>
  3.  <script>
  4. function checkTime(){
  5.             var startTimeValue = document.getElementById("startTime");
  6.             var endTimeValue = document.getElementById("endTime");
  7.  
  8.             if( startTimeValue.options[startTimeValue.selectedIndex].value > endTimeValue.options[endTimeValue.selectedIndex].value) {
  9.                 alert("End Time should be greater than Start Time");
  10.                 return false;
  11.             }
  12.  
  13.             return true;
  14.  
  15.         }
  16.  
  17. </script>
  18.  <BODY>
  19.   <table cellpadding="0" cellspacing="0" border="0" >
  20.                                 <tr>
  21.                                 <td> Start Time:</td>
  22.                                     <td>
  23.                                         <select style="font-size: 9pt;" id="startTime" onchange="checkTime()">
  24.                                             <option value="1">1</option>
  25.                                             <option value="2">2</option>
  26.                                             <option value="3">3</option>
  27.                                             <option value="4">4</option>
  28.                                             <option value="5">5</option>
  29.                                             <option value="6">6</option>
  30.                                             <option value="7">7</option>
  31.                                             <option value="8">8</option>
  32.                                             <option value="9">9</option>
  33.                                             <option value="10">10</option>
  34.                                             <option value="11">11</option> 
  35.                                             <option value="12">12</option>                                                                                                                                                                                 
  36.                                         </select></td>
  37.                                         <td>
  38.                                             <select dataSource="actionForm.fromMinutes" style="font-size: 9pt;">
  39.                                                 <option value="0">00</option>
  40.                                                 <option value="1">01</option>
  41.                                                 <option value="2">02</option>
  42.                                                 <option value="3">03</option>
  43.                                                 <option value="4">04</option>
  44.                                                 <option value="5">05</option>
  45.                                                 <option value="6">06</option>
  46.                                                 <option value="7">07</option>
  47.                                                 <option value="8">08</option>
  48.                                                 <option value="9">09</option>
  49.                                                 <option value="9">10</option>
  50.                                                 <option value="9">11</option>
  51.                                                 <option value="9">12</option>
  52.                                                 <option value="9">13</option>
  53.                                                 <option value="9">14</option>
  54.                                                 <option value="9">15</option>
  55.                                                 <option value="9">16</option>
  56.                                                 <option value="9">17</option>
  57.                                                 <option value="9">18</option>
  58.                                                 <option value="9">19</option>
  59.                                                 <option value="9">20</option>
  60.                                                 <option value="9">21</option>
  61.                                                 <option value="9">22</option>
  62.                                                 <option value="9">23</option>
  63.                                                 <option value="9">24</option>
  64.                                                 <option value="9">25</option>
  65.                                                 <option value="9">26</option>
  66.                                                 <option value="9">27</option>
  67.                                                 <option value="9">28</option>
  68.                                                 <option value="9">29</option>
  69.                                                 <option value="9">30</option>
  70.                                                 <option value="9">31</option>
  71.                                                 <option value="9">32</option>
  72.                                                 <option value="9">33</option>
  73.                                                 <option value="9">34</option>
  74.                                                 <option value="9">35</option>
  75.                                                 <option value="9">36</option>
  76.                                                 <option value="9">37</option>
  77.                                                 <option value="9">38</option>
  78.                                                 <option value="9">39</option>
  79.                                                 <option value="9">40</option>
  80.                                                 <option value="9">41</option>
  81.                                                 <option value="9">42</option>
  82.                                                 <option value="9">43</option>
  83.                                                 <option value="9">44</option>
  84.                                                 <option value="9">45</option>
  85.                                                 <option value="9">46</option>
  86.                                                 <option value="9">47</option>
  87.                                                 <option value="9">48</option>
  88.                                                 <option value="9">49</option>
  89.                                                 <option value="9">50</option>
  90.                                                 <option value="9">51</option>
  91.                                                 <option value="9">52</option>
  92.                                                 <option value="9">53</option>
  93.                                                 <option value="9">54</option>
  94.                                                 <option value="9">55</option>
  95.                                                 <option value="9">56</option>
  96.                                                 <option value="9">57</option>
  97.                                                 <option value="9">58</option>
  98.                                                 <option value="9">59</option>
  99.                                                 <option value="9">60</option>
  100.  
  101.                         </select>
  102.                                         </td>
  103.                                         <td>
  104.                                             <select dataSource="actionForm.startampm" style="font-size: 9pt;">
  105.                                                    <option value="0">AM</option>
  106.                                                  <option value="1">PM</option>                                          
  107.                                             </select>
  108.                                         </td>
  109.  
  110.                                     <td>&nbsp; End Time:</td>
  111.                                       <td style="padding-left: 10px;">
  112.                                             <select style="font-size: 9pt;" id="endTime" onchange="checkTime()">
  113.                                                 <option value="1">1</option>
  114.                                                 <option value="2">2</option>
  115.                                                 <option value="3">3</option>
  116.                                                 <option value="4">4</option>
  117.                                                 <option value="5">5</option>
  118.                                                 <option value="6">6</option>
  119.                                                 <option value="7">7</option>
  120.                                                 <option value="8">8</option>
  121.                                                 <option value="9">9</option>
  122.                                                 <option value="10">10</option>
  123.                                                 <option value="11">11</option>
  124.                                                 <option value="12">12</option>                                                                                                                                                                                 
  125.                                             </select>
  126.                                         </td>
  127.                                           <td>
  128.                                             <select dataSource="actionForm.toMinutes" style="font-size: 9pt;">
  129.                                                 <option value="0">00</option>
  130.                                                 <option value="1">01</option>
  131.                                                 <option value="2">02</option>
  132.                                                 <option value="3">03</option>
  133.                                                 <option value="4">04</option>
  134.                                                 <option value="5">05</option>
  135.                                                 <option value="6">06</option>
  136.                                                 <option value="7">07</option>
  137.                                                 <option value="8">08</option>
  138.                                                 <option value="9">09</option>
  139.                                                 <option value="9">10</option>
  140.                                                 <option value="9">11</option>
  141.                                                 <option value="9">12</option>
  142.                                                 <option value="9">13</option>
  143.                                                 <option value="9">14</option>
  144.                                                 <option value="9">15</option>
  145.                                                 <option value="9">16</option>
  146.                                                 <option value="9">17</option>
  147.                                                 <option value="9">18</option>
  148.                                                 <option value="9">19</option>
  149.                                                 <option value="9">20</option>
  150.                                                 <option value="9">21</option>
  151.                                                 <option value="9">22</option>
  152.                                                 <option value="9">23</option>
  153.                                                 <option value="9">24</option>
  154.                                                 <option value="9">25</option>
  155.                                                 <option value="9">26</option>
  156.                                                 <option value="9">27</option>
  157.                                                 <option value="9">28</option>
  158.                                                 <option value="9">29</option>
  159.                                                 <option value="9">30</option>
  160.                                                 <option value="9">31</option>
  161.                                                 <option value="9">32</option>
  162.                                                 <option value="9">33</option>
  163.                                                 <option value="9">34</option>
  164.                                                 <option value="9">35</option>
  165.                                                 <option value="9">36</option>
  166.                                                 <option value="9">37</option>
  167.                                                 <option value="9">38</option>
  168.                                                 <option value="9">39</option>
  169.                                                 <option value="9">40</option>
  170.                                                 <option value="9">41</option>
  171.                                                 <option value="9">42</option>
  172.                                                 <option value="9">43</option>
  173.                                                 <option value="9">44</option>
  174.                                                 <option value="9">45</option>
  175.                                                 <option value="9">46</option>
  176.                                                 <option value="9">47</option>
  177.                                                 <option value="9">48</option>
  178.                                                 <option value="9">49</option>
  179.                                                 <option value="9">50</option>
  180.                                                 <option value="9">51</option>
  181.                                                 <option value="9">52</option>
  182.                                                 <option value="9">53</option>
  183.                                                 <option value="9">54</option>
  184.                                                 <option value="9">55</option>
  185.                                                 <option value="9">56</option>
  186.                                                 <option value="9">57</option>
  187.                                                 <option value="9">58</option>
  188.                                                 <option value="9">59</option>
  189.                                                 <option value="9">60</option>
  190.  
  191.  
  192.                                                  </select>
  193.                                             </td>
  194.                                     <td>
  195.                                         <select dataSource="actionForm.endampm" style="font-size: 9pt;">
  196.                                               <option value="0">AM</option>
  197.                                              <option value="1">PM</option>
  198.                                         </select>                                       
  199.                                     </td>
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.                                         </tr>
  207.                             </table>
  208.  </BODY>
  209. </HTML>
  210.  
  211.  
Oct 24 '08 #1
Share this Question
Share on Google+
1 Reply


P: 55
the value of the select boxes is a string right now, try this:


Expand|Select|Wrap|Line Numbers
  1. var start=startTimeValue.options[startTimeValue.selectedIndex].value*1
  2. var end=endTimeValue.options[endTimeValue.selectedIndex].value*1
  3. if( start > end) 
javascript will try to preform the math (*1), and will change the "string" to actual numbers if there aren't any letters in it:) There is a little more logic involved for you to make this work right, but that should get you started:)
Oct 24 '08 #2

Post your reply

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