1

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;
});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
user244394
  • 13,168
  • 24
  • 81
  • 138

2 Answers2

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*/}
}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 1
    This doesn't use proper calendaring and check the days in a month. – Timothy Gonzalez Jun 10 '17 at 23:53
  • @TimothyGonzalez the OP specifically asks to validate each field on its own. – Gabriele Petrioli Jun 10 '17 at 23:59
  • 1
    Perhaps the OP is asking the wrong question incorrectly. If you are dealing with dates you can't escape the fact that the fields are related. It doesn't answer the intent of the question, which is determining age. How old am I if I was born on 04/31/2017 (April 31 2017 a non-existent date)? – Timothy Gonzalez Jun 11 '17 at 00:04
  • @TimothyGonzalez rolled back to your previous edit as it address the comment. The ` > 31` still has the same problem of getting wrong dates, eventhough JS will allow that and wrap the date to the next month. – Gabriele Petrioli Jun 11 '17 at 00:22
0

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;
};
MANAN
  • 713
  • 9
  • 6