Solved

validate time field

Posted on 2008-10-22
4
351 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

810 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