I'm doing something similar to this example:
http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-callback

I have a date input field, and if you are under 13, it pops up another
field for parent's age.But unlike this example,I want the parent to be
required to be 18 or older.How would I set it up to require the input,
and require it to be over a certain age.

It 's also important to note that the inputs are dates, not ages, so I
can't just do "min:18" because it 's more like "Tue Aug 29 2023
11:18:38 GMT-0700 (Pacific Daylight Time)"


If needed, here's what I am currently doing:

function checkAge(parent) {
  var min_age = 13;

  if (parent) {
    var dateArray = parent.split('/');
    min_age = 18;
  } else {
    var dateArray = $('#dateOfBirth').val().split('/');
  }
  var year = dateArray[2];
  var month = dateArray[0] - 1;
  var day = dateArray[1];

  var theirDate = new Date();
  theirDate.setFullYear((parseInt(year) + min_age), month, day);
  console.log(theirDate);
  var today = new Date();
  console.log(today);
  console.log(today - theirDate);

  if (today < theirDate) {
    $('#parentsRequired').show();
    return true;
  } else {
    $('#parentsRequired').hide();
    return false;
  }
}

....

dateOfBirth: {
  required: function(element) {
    var check = checkAge();
    return true;
  },
  date: true
},
parentsDateOfBirth: {
  required: function(element) {
    return checkAge($(element).val());
  },
  date: true
},

Reply via email to