Question

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?

Was it helpful?

Solution

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."
      }
 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top