且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript月份和日期和年份检查

更新时间:2023-01-28 23:41:26

您应该使用以下方法验证输入确实是数字的,然后检查从

You should use the following method to validate if the input is indeed numeric, and then check the ranges

-isnumeric>使用JavaScript验证数字 - IsNumeric()

copied from Validate numbers in JavaScript - IsNumeric()

function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

使用这样(阅读输入后的

And use it like this (after reading the inputs)

if (!IsNumeric(day) || (day < 1) || (day > 31)) 
   {/*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*/}