views:

680

answers:

1

I have this condition in my validation:

consent: {
   required: function(element) {
     var age = getAge($("#birthdate").val());
     if(age >= 18){
      return false;
     } else if(age >= 13 && age <=17 ){
      $('#consent:checked');
     } else {
      //should show error message not ligible to register
     }
    }
}

how can return a message from above condition?

+3  A: 

I would create a special rule for each condition with an associated message. For age, it must be greater than 13. For consent it is required but only if age is less than 18.

 $.validator.addMethod( 'minimumAge', function(value,element) {
     return getAge(element) >= value;
 });
 $('form').validate({
      rules: {
          age: minimumAge(13),
          consent: {
              required: function(element) {
                               return getAge($('#birthDate') < 18;
                        }
          }
      }
      messages: {
          age: "You must be older than 13 to register.",
          consent: "If you are under 18 your must have your parent's consent to register."
      }
 });
tvanfosson
thanks for your answer, but the field that handles the age is a textbox wherein the users enter their birthdate. so the user is not entering the their age i have the function getAge to compute their age.
christian
So adjust the code so that it checks a minimum year and does a calculation of their current age based on the current year and the date in the text box.
tvanfosson
Thanks! The "depends:" causes an error removing that will OK the script.
christian