You are viewing this page out of context. To see it in the context it is intended please click here.
About This Page
Reply posted by Chris Key on Wed 1 May 2002 in response to Form Validator R2.0
Re: Validate a time value : hh:mm am/pm
Back when I didn't really know the object model, I developed this approach toaccess element values. Thanks to Uri Grinwald for cleaning up my syntax and
using the object model to its fullest.
In my posting you have to pass the literal field name so quotes are required.
Here you pass the field, no quotes:
function checkTime(timeV) {
var textTime = document.myForm.elements[timeV.name].value;
var timeField = document.myForm.elements[timeV.name]
var timeExpression =/^(\d{2}):(\d{2})\s{0,1})([AM|PM|am|pm]{2,2}))\W{0}$/;
var valresult = textTime.match(timeExpression);
if (valresult==null) {
alert("Time entry is invalid. Please enter a valid time in hh:mm AM or PM
format (e.g. 08:00 AM)");
timeField.value = "";
timeField.focus();
}
}
Notice Uri allows for entry of upper AND lower case AM/am or PM/pm. He is much
too kind to his users. Me, I am just giving payback for all those requirements
designed for which were never needed.