Solved

validate time field

Posted on 2008-10-22
4
350 Views
Last Modified: 2012-06-21
I have fields on my web page where users need to enter time in hours and minutes in hh:mm format
e.g <input name="START_MON" type="text" id="START_MON" value="<%= DoDateTime((Recordset1.Fields.Item("START_MON").Value), 4, 1033) %>"  size="10" maxlength="5">.
Is there a way of inserting a validation to showing an alert if the time is entered in the wrong format

 Thank you
0
Comment
Question by:Camnoc
4 Comments
 
LVL 16

Assisted Solution

by:brad2575
brad2575 earned 30 total points
ID: 22777991
you can use regular expressions.

and use this as the regular expression that you pass to it:
^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$

and then call this function
// this field checks retular expressions and takes the following
// RegExp = Regular Expression to check against
// checkField = the field being checked
// checkValue = the value validing the RegExp against
// ErrorMsg = error to display
function regularExpCheck(RegExp, checkField, checkValue, ErrorMsg){
      //valid            
      if (RegExp.test(checkValue)){            
            return (true);
      }
      
      //not valid, give error, set focus and give error
      alert(ErrorMsg);
      checkField.focus();
      return (false);
      
}
0
 
LVL 15

Assisted Solution

by:Tomarse111
Tomarse111 earned 200 total points
ID: 22778015
Presuming you have a submit button to validate this form, add this function and then add an onClick event with the following onClick(return ValidateTime(this)). If the time does doesn't match a hh:mm format (24hr format as well), then it will return false, stop the p[age submission and trigger one of the alerts.

:)


function ValidateTime(Input)

{

	var regex = /^([0-9]{1,2})[:. ]([0-9]{1,2})$/;

	var matches = Input.value.match(regex);

	if ( matches != null )

	{

		if ( matches[1] < 0 || matches[1] > 24 )

		{

			alert( "Please enter a valid 24hr time\n\nThe hour must be between 00 and 24" );

			return false;

		}

		if ( matches[2] < 0 || matches[2] > 59 )

		{

			alert( "Please enter a valid 24hr time\n\nThe minute must be between 00 and 59" );

			return false;

		}

	}

	else

	{

		alert( "Please enter a valid 24hr time format (hh:mm)" );

		return false;

	}

	return true;

}

Open in new window

0
 
LVL 16

Accepted Solution

by:
sh0e earned 270 total points
ID: 22778197
I didn't include 24:00, as it's usually just shown as 0:00.
Rarely does anyone refer to it as 24:00, but it's easy to add in.
<script type="text/javascript">

function val12hr(){

  var ret = document.getElementById('START_MON').value.match(/^(0?[0-9]|^1[0-2])\:[0-5][0-9]$/);

  if(ret) return true;

  else {alert('Invalid 12 hour time'); return false; }

}

function val24hr(){

  var ret = document.getElementById('START_MON').value.match(/^([0-1]?[0-9]|^2[0-3])\:[0-5][0-9]$/);

  if(ret) return true;

  else { alert('Invalid 24 hour time'); return false; }

}

</script>

<input name="START_MON" type="text" id="START_MON" value="<%= DoDateTime((Recordset1.Fields.Item("START_MON").Value), 4, 1033) %>"  size="10" maxlength="5" onBlur="val12hr()"></input>

Open in new window

0
 

Author Closing Comment

by:Camnoc
ID: 31508793
Thank you
Excellent
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now