最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

validation - javascript month and day and year check - Stack Overflow

programmeradmin2浏览0评论

I am trying to check for MM /DD /YYYY.

currently my script fails when you enter 0 / 0 /0 or 00/00/0000. I am trying to check that the user is over 21 and must enter a valid two digit month, two digit day and 4 digit year.

Any suggestions?

$("#gate-box").submit(function() {

    var day = $("#day").val();
    var month = $("#month").val();
    var year = $("#year").val();

    var age = 21;

    var mydate = new Date();
    mydate.setFullYear(year, month - 1, day);

    var currdate = new Date();
    currdate.setFullYear(currdate.getFullYear() - age);
    if ((currdate - mydate) < 0) {        
        $.msg("Sorry, you must be at least " + age + " to enter.");
        return false;
    }
    else if (month > 12) {
        $('#month').css({ 'border': '1px solid red' });
        $.msg("Please enter a valid month.");
        $('#month').focus();
        return false;
    }
    else if (day > 31) {
        $('#month').css({ 'border': 'none' });
        $('#day').css({ 'border': '1px solid red' });
        $.msg("Please enter a valid day.");
        $('#day').focus();
        return false;
    }
    else if (month.length === 0 || day.length === 0 || year.length === 0) {
        $('#day').css({ 'border': 'none' });
        $.msg("Please enter all fields.");
        return false;
    }

    if ((currdate - mydate) > 0) {
        $.colorbox.close()
        $.setCookie('diageoagecheck', 'verified', { duration: 3 });
    }
    return false;
});

I am trying to check for MM /DD /YYYY.

currently my script fails when you enter 0 / 0 /0 or 00/00/0000. I am trying to check that the user is over 21 and must enter a valid two digit month, two digit day and 4 digit year.

Any suggestions?

$("#gate-box").submit(function() {

    var day = $("#day").val();
    var month = $("#month").val();
    var year = $("#year").val();

    var age = 21;

    var mydate = new Date();
    mydate.setFullYear(year, month - 1, day);

    var currdate = new Date();
    currdate.setFullYear(currdate.getFullYear() - age);
    if ((currdate - mydate) < 0) {        
        $.msg("Sorry, you must be at least " + age + " to enter.");
        return false;
    }
    else if (month > 12) {
        $('#month').css({ 'border': '1px solid red' });
        $.msg("Please enter a valid month.");
        $('#month').focus();
        return false;
    }
    else if (day > 31) {
        $('#month').css({ 'border': 'none' });
        $('#day').css({ 'border': '1px solid red' });
        $.msg("Please enter a valid day.");
        $('#day').focus();
        return false;
    }
    else if (month.length === 0 || day.length === 0 || year.length === 0) {
        $('#day').css({ 'border': 'none' });
        $.msg("Please enter all fields.");
        return false;
    }

    if ((currdate - mydate) > 0) {
        $.colorbox.close()
        $.setCookie('diageoagecheck', 'verified', { duration: 3 });
    }
    return false;
});
Share Improve this question edited Oct 6, 2010 at 21:12 Gabriele Petrioli 196k34 gold badges271 silver badges328 bronze badges asked Oct 6, 2010 at 20:10 user244394user244394 13.5k25 gold badges84 silver badges142 bronze badges 3
  • try validating the inputs before using them to create a date object.. – Gabriele Petrioli Commented Oct 6, 2010 at 20:15
  • how would go about checking for two digit month, two digit day and 4 digit year. currently the script fails when you enter 0 / 0 /0 or 00/00/0000. – user244394 Commented Oct 6, 2010 at 20:26
  • check if they fall inside the accepted range of (1-31), (1-12), (1900-2100) – Gabriele Petrioli Commented Oct 6, 2010 at 20:37
Add a ment  | 

2 Answers 2

Reset to default 3

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

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) 
    {/*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*/}
}

I think it is fairly straight forward, just handling few edge cases can be tricky. Here is my typescript version to validate month and day and year

const isValidDate = (day: string, month: string, year: string): boolean => {
  const parsedDay = parseInt(day, 10);
  const parsedMonth = parseInt(month, 10);
  const parsedYear = parseInt(year, 10);

  if (
    isNaN(parsedDay) ||
    isNaN(parsedMonth) ||
    isNaN(parsedYear) ||
    parsedMonth < 1 ||
    parsedMonth > 12 ||
    parsedDay < 1
  ) {
    return false;
  }

  const maxDaysInMonth = new Date(parsedYear, parsedMonth, 0).getDate();

  return parsedDay <= maxDaysInMonth;
};
发布评论

评论列表(0)

  1. 暂无评论