Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate For Military Time

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello, does anyone have a good snippet on validating if a field is in military time format?

Thanks!
 

<HTML>
<head>
<SCRIPT language=&quot;javascript&quot; type=&quot;text/javascript&quot;>
// anyMask found on web library
function anyMask(event, sMask) {
var KeyTyped = String.fromCharCode(getKeyCode(event));
var targ = getTarget(event);
keyCount = targ.value.length;

if (sMask.charAt(keyCount) == '*')
return true;

if (sMask.charAt(keyCount) == KeyTyped) {
return true;
}

if ((sMask.charAt(keyCount) == '#') && isNumeric(KeyTyped))
return true;

if ((sMask.charAt(keyCount) == 'A') && isAlpha(KeyTyped))
return true;

if ((sMask.charAt(keyCount) == '?') && isPunct(KeyTyped))
return true;
if (KeyTyped.charCodeAt(0) < 32) return true;

return false;
}

function getTarget(e) {
// IE5
if (e.srcElement) {
return e.srcElement;
}
if (e.target) {
return e.target;
}
}

function getKeyCode(e) {
//IE5
if (e.srcElement) {
return e.keyCode
}
// NC5
if (e.target) {
return e.which
}
}

function isNumeric(c)
{
var sNumbers = &quot;01234567890&quot;;
if (sNumbers.indexOf(c) == -1)
return false;
else return true;

}

function isAlpha(c)
{
var lCode = c.charCodeAt(0);
if (lCode >= 65 && lCode <= 122 )
{
return true;
}
else
return false;
}

function isPunct(c)
{
var lCode = c.charCodeAt(0);
if (lCode >= 32 && lCode <= 47 )
{
return true;
}
else
return false;

}

</SCRIPT>

<SCRIPT language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function IsValidTime(fldName,timeStr) {
/* Checks if time is in HH.MM format.
Takes no separator so client can use numeric pad entirely.*/

/* check for no entry or < 4 characters
completely blank is OK for this routine,
if a required field, other means must enforce*/
mhour = timeStr.substring(0,2)
mmin = timeStr.substring(2,4)
if (mhour==&quot;&quot; && mmin==&quot;&quot;) {
return true;
}
/*check if time entry is less than four numbers -
if so it is not acceptable */
if (timeStr.length < 4) {
alert(&quot;Use four numbers - military time format - e.g 1536&quot;);
fldName.value=&quot;&quot;;
fldName.focus();
fldName.select();
return false;
}

if (mhour < 0 || mhour > 23) {
alert(&quot;24 hour format -must be between 0 and 23)&quot;);
fldName.value=&quot;&quot;;
fldName.focus();
fldName.select();
return false;
}

if (mmin<0 || mmin > 59) {
alert (&quot;Minute must be between 0 and 59.&quot;);
fldName.value=&quot;&quot;;
fldName.focus();
fldName.select();
return false;
}

fldName.value = mhour+&quot;:&quot;+mmin
return true;
}
</SCRIPT>

<TITLE> Testing military time validation
</TITLE>
</HEAD>
<BODY>
<CENTER>
<FORM name=&quot;timeform&quot; method=&quot;post&quot; action=&quot;junk.asp&quot;>
Time (HH.MM 24 hour format)
<INPUT type=&quot;text&quot; name=&quot;time&quot; size=&quot;5&quot; maxlength=&quot;5&quot;
onkeypress='return anyMask(event, &quot;####&quot;);'
onblur = &quot;return IsValidTime(this,this.value);&quot;>
<BR>
Other Stuff:
<INPUT type=&quot;text&quot; name=&quot;junk&quot; size=&quot;30&quot;>
<INPUT type=&quot;submit&quot; value=&quot;Submit&quot;>
</FORM>
</CENTER>
</BODY>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top