tags:

views:

63

answers:

2
var validator = $("#fullRegForm").validate({
    errorLabelContainer: "#error_container",
    wrapper: "li",
    errorClass: "reg_error",
    rules: {
        fr_birthdate: {
            date: true,
            required: true,
            minimumAge: true
        }, 
        fr_consent: {
          required: function(element) {
              var age = getAge($("#fr_birthdate").val());
              if(age >=13 && age < 18){
                   $("#reg_consent").show();
              } else {
                  $("#reg_consent").hide();
              }
          }
        }
   },
    messages: {
    fr_birthdate: {
     date: "Please enter a valid date of birth",
     required: "Please enter date of birth",
     minimumAge: "under 13 years old are not allowed"
    }, 
    fr_consent: "13-17 years of age must have a parental consent"
   }

I uses a container to display errors in the form.. but the message for fr_consent message still displaying even the age input is over 18 years old. I try using the same rules but not using error container and it works perfectly. Did I missed something on my code?

getAge:

function getAge(birthDate) {
    var raw_date = birthDate;
    var dobArr = raw_date.split("/");
    var dob = new Date();
    dob.setFullYear(dobArr[2], dobArr[0]-1, dobArr[1]);

    var now = new Date();

    function isLeap(year) {
    return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
    }

    // days since the birthdate    
    var days = Math.floor((now.getTime() - dob.getTime())/1000/60/60/24);
    var age = 0;
    // iterate the years
    for (var y = dob.getFullYear(); y <= now.getFullYear(); y++){
    var daysInYear = isLeap(y) ? 366 : 365;
        if (days >= daysInYear){
          days -= daysInYear;
          age++;
          // increment the age only if there are available enough days for the year.
        }
    }
    return age;
}
A: 

try:

 var age = getAge($("#fr_birthdate").val() * 1);
 if(age < 18){
    if(age >= 13){
       $("#reg_consent").show();
    }
 }
TheVillageIdiot
A: 

Ok, thanks for pasting the getAge code, seems to work fine.

The function for required probably needs to return a boolean. Javascript returns, I think, false by default. So at the moment you're always returning false

if(age >=13 && age < 18){
               $("#reg_consent").show();
               return false;
          } else {
              $("#reg_consent").hide();
              return true;
          }
David Archer
Thanks for your answer I will give it a try when i got home thanks.
text