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"
}
我使用容器來顯示窗體中的錯誤..但fr_consent消息的消息仍然顯示年齡輸入超過18歲。我嘗試使用相同的規則,但不使用錯誤容器,並且它完美地工作。我錯過了我的代碼?jquery依賴性驗證
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;
}
getAge()是什麼樣的? – 2009-08-04 10:50:48