Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time Verification

    I have 2 text fields:
    <input type="text" name="startTime">
    <input type="text" name="endTime">

    For the start time I want to verify that they select a time between
    7:00am and 11:30 am.

    For the end time I want to verify that they select a time between
    12:30 pm and 6:00 pm

    It's seems like something similiar to what is below should be pretty close, but I have not been able to get it to work. Can anyone help?

    Code:
    <script language="javascript">
    
    	function chkForm() {
    		var startTime = document.frmOrder.startTime.value;
    		var endTime = document.frmOrder.endTime.value;
    	
    		// Check start time
    		if (startTime.value < 7:00 && > 11:30 ) {
    			alert('This time is not valid');
    			return false
    		}		
    
    	}
    </script>
    Last edited by bspahr; 10-18-2006 at 10:38 PM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    There are three things wrong with your test.

    The first is that you haven't specified what should be tested for greater than 11:30

    Code:
    if (startTime.value < 7:00 && startTime.value > 11:30 ) {
    Second you are testing for if it is bogth less than one value and greater than the other and it can't be both at the same time.

    Code:
    if (startTime.value < 7:00 || startTime.value > 11:30 ) {
    Finally 7:00 and 11:30 are not valud as numbers because of the colon in the middle and they are not valid as strings because they are not enclosed in quoutes and they are not valid as times because they are not part of a date/time object. If you enclose them in quotes then they will be valid strings for the test but 8:9999999 will be within the acceptable range even though it is not actually able to be interpreted as a time. You will need to add validation for the field being entered as a correct hh:mm format

    Code:
    var tz = startTime.value.split(':');
    if (tz.length != 2) {alert('This time is not valid'); return false;}
    if (Number(tz[0]) < 1 || Number(tz[0]) > 12) {alert('This time is not valid'); return false;}
    if (Number(tz[1]) > 59) {alert('This time is not valid'); return false;}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the response. This is sort of working. It still posts the form if the enter a time higher than 11:30 or before 7:00 though. The start time entered must be between 7:00am and 11:30 am. Same thing for the end time (between 12:30 and 6:00 pm). Also, neither of these fields are required. How can I let them post the form even if the don't enter anything?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •