validate time field

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
CamnocAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brad2575Commented:
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
Tomarse111Commented:
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
sh0eCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamnocAuthor Commented:
Thank you
Excellent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.