如果我理解正确,那么这个解决方案应该可以满足您的要求。
输入时有 3 种状态。
红色 - 绝对无效
黄色 - 部分有效
绿色 - 绝对有效
HTML
<input id="date" type="text" maxlength="10" />
Javascript
/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global global */
(function (global) {
"use strict";
global.addEventListener("load", function onLoad() {
global.removeEventListener("load", onLoad);
document.getElementById("date").addEventListener("keyup", function (evt) {
var target = evt.target,
value = target.value,
parts = value.split("/"),
day = parseInt(parts[0], 10),
month = parseInt(parts[1], 10) - 1,
date = new Date(parseInt(parts[2], 10) || 1600, month, day),
dateCheck = day === date.getDate() && month === date.getMonth(),
finalCheck = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(18|19|20)\d\d$/.test(value) && dateCheck;
if (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}$/.test(value) || (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}(0[1-9]|1[012])\/{0,1}$/.test(value) && dateCheck) || finalCheck) {
if (finalCheck) {
target.style.backgroundColor = "green";
} else {
target.style.backgroundColor = "yellow";
}
} else {
target.style.backgroundColor = "red";
}
}, false);
}, false);
}(window));
在jsfiddle 上
更新:解决以下评论中的其他问题
Javascript
/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global global */
(function (global) {
"use strict";
global.addEventListener("load", function onLoad() {
global.removeEventListener("load", onLoad);
document.getElementById("date").addEventListener("keyup", function (evt) {
var target = evt.target,
value = target.value,
parts = value.split("/"),
day = parseInt(parts[0], 10),
month = parseInt(parts[1], 10) - 1,
date = new Date(parseInt(parts[2], 10) || 1600, month, day),
dateCheck = day === date.getDate() && month === date.getMonth(),
finalCheck = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(18|19|20)\d\d$/.test(value) && dateCheck;
if (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}$/.test(value) || (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}(0[1-9]|1[012])\/{0,1}$/.test(value) && dateCheck) || finalCheck) {
if (finalCheck) {
target.style.backgroundColor = "green";
} else {
target.style.backgroundColor = "yellow";
}
} else {
target.style.backgroundColor = "red";
if (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}(0[1-9]|1[012])\/{0,1}$/.test(value)) {
if (!dateCheck) {
alert("incorrect number of days for month");
}
}
}
}, false);
}, false);
}(window));
在jsfiddle上
更新:以及可用于与旧的非标准 Internet Explorer 兼容的事件侦听器函数示例
使用它来添加监听器
function addEvt(obj, type, fni) {
if (obj.attachEvent) {
obj['e' + type + fni] = fni;
obj[type + fni] = function () {
obj['e' + type + fni](window.event);
};
obj.attachEvent('on' + type, obj[type + fni]);
} else {
obj.addEventListener(type, fni, false);
}
}
使用它来删除监听器
function removeEvt(obj, type, fni) {
if (obj.detachEvent) {
obj.detachEvent('on' + type, obj[type + fni]);
obj[type + fni] = nul;
} else {
obj.removeEventListener(type, fni, false);
}
}
正在使用
/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global global */
(function (global) {
"use strict";
function addEvt(obj, type, fni) {
if (obj.attachEvent) {
obj['e' + type + fni] = fni;
obj[type + fni] = function () {
obj['e' + type + fni](window.event);
};
obj.attachEvent('on' + type, obj[type + fni]);
} else {
obj.addEventListener(type, fni, false);
}
}
function removeEvt(obj, type, fni) {
if (obj.detachEvent) {
obj.detachEvent('on' + type, obj[type + fni]);
obj[type + fni] = nul;
} else {
obj.removeEventListener(type, fni, false);
}
}
addEvt(global, "load", function onLoad() {
removeEvt(global, "load", onLoad);
addEvt(document.getElementById("date"), "keyup", function (evt) {
var target = evt.target,
value = target.value,
parts = value.split("/"),
day = parseInt(parts[0], 10),
month = parseInt(parts[1], 10) - 1,
date = new Date(parseInt(parts[2], 10) || 1600, month, day),
dateCheck = day === date.getDate() && month === date.getMonth(),
finalCheck = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(18|19|20)\d\d$/.test(value) && dateCheck;
if (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}$/.test(value) || (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}(0[1-9]|1[012])\/{0,1}$/.test(value) && dateCheck) || finalCheck) {
if (finalCheck) {
target.style.backgroundColor = "green";
} else {
target.style.backgroundColor = "yellow";
}
} else {
target.style.backgroundColor = "red";
if (/^(0[1-9]|[12][0-9]|3[01])\/{0,1}(0[1-9]|1[012])\/{0,1}$/.test(value)) {
if (!dateCheck) {
alert("incorrect number of days for month");
}
}
}
});
});
}(window));
在jsfiddle 上