您应该使用以下方法来验证输入是否确实是数字,然后检查范围
复制自
Validate numbers in JavaScript - IsNumeric()
function IsNumeric(input)
{
return (input - 0) == input && input.length > 0;
}
像这样使用(
读取输入后
)
if (!IsNumeric(day) || day < 1)
{/*handle wrong day here and return false*/}
if (!IsNumeric(month) || (month < 1) || (month > 12))
{/*handle wrong month here and return false*/}
if (!IsNumeric(year) || (year < 1900) || (year > 2100))
{/*handle wrong year here and return false*/}
var lastDayOfMonth = new Date(year, parseInt(month) + 1, -1).getDate();
if (day > lastDayOfMonth) {
{/*handle wrong year here and return false*/}
}