<script language=javascript>
function VerifyTime(id, type) {
str = document.all(id).value;
str = str.replace(/\s*/, "");
str = str.replace(/\s*$/, "");
document.all(id).value = str; //这几句只是用于将字符串首尾的空格去掉
if (str.length == 0) return;
switch (type) {
case "hour":
str = str.match(/^(?:0?[0-9]|1[0-9]|2[0-4])$/);
if (str == null) alert("小时格式不正确");
return;
case "minute":
str = str.match(/^[0-5]?[0-9]$/);
if (str == null) alert("分钟格式不正确");
return;
default:
alert("类型不正确");return;
}
}
</script>
...
<input id="hours" type="text" onchange='VerifyTime(this.id, "hour");'><b>:</b><input id="minutes" type="text" onchange='VerifyTime(this.id, "minutes");'>