Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

validate time field

Posted on 2008-10-22
4
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Assisted Solution

by:brad2575
brad2575 earned 120 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 800 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 1080 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

704 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