var timeExpression uses Regular Expression to define the input pattern, hh:mm
AM (or PM), to match. Using the Reg Exp match event, valresult will return null
if the pattern is not fulfilled.
<script>function checkTime(timeV,message) {
var textTime = document.forms[0].elements[timeV].value;
var timeField = document.forms[0].elements[timeV]
var timeExpression = /^(\d{2}):(\d{2})\s{1}([AP]M)$/;
var valresult = textTime.match(timeExpression);
if (valresult==null) {
alert("Time entry in " + message + " is invalid. Please enter a valid
time in hh:mm AM or PM format (e.g. 08:00 AM)");
timeField.value = "";
timeField.focus();
}
}
</script>
In your time field onChange event, pass the field name and a message (to
customize the alert for the multiple time fields).
Although I didn't in this example, on a valid pattern match (i.e. valresult is
not NULL) you can use the array of grouped patterns (defined by parans
grouping in the Regular Expression). returned from the above Regular Expression
valresult. So, valresult[1] = hh, valresult[2] = mm valresult[3]= AM or PM
(valresult[0] is the entire string). With hh and mm segmented, now I can
validate to make sure hh < 24 and mm < 60.
Finally, I can give a little back ...
var timeExpression uses Regular Expression to define the input pattern, hh:mm AM (or PM), to match. Using the Reg Exp match event, valresult will return null if the pattern is not fulfilled.
<script>function checkTime(timeV,message) { var textTime = document.forms[0].elements[timeV].value; var timeField = document.forms[0].elements[timeV] var timeExpression = /^(\d{2}):(\d{2})\s{1}([AP]M)$/; var valresult = textTime.match(timeExpression); if (valresult==null) { alert("Time entry in " + message + " is invalid. Please enter a valid time in hh:mm AM or PM format (e.g. 08:00 AM)"); timeField.value = ""; timeField.focus(); } } </script>
In your time field onChange event, pass the field name and a message (to customize the alert for the multiple time fields).
Although I didn't in this example, on a valid pattern match (i.e. valresult is not NULL) you can use the array of grouped patterns (defined by parans grouping in the Regular Expression). returned from the above Regular Expression valresult. So, valresult[1] = hh, valresult[2] = mm valresult[3]= AM or PM (valresult[0] is the entire string). With hh and mm segmented, now I can validate to make sure hh < 24 and mm < 60.