Solved

validate time field

Posted on 2008-10-22
4
347 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you
Excellent
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now