当我在三个不同的文本字段中分割时,我一直在寻找确定出生日期的明确方法。到目前为止,我有一个输入字段正确处理,但我无法更改html代码。我仍然需要这个符合3文本字段组。我读取使用隐藏的输入字段验证在出生日期的三个字段中输入的值可以是一种解决方案。任何人都可以帮我解决这个问题。
这是我的jsfiddle:http://jsfiddle.net/b8fubr5u/1/
$(document).ready(function () {
$.validator.addMethod("dob", function (value, element) {
var result = true;
var ageMin = 18;
var ageMax = 85;
//is the date valid?
//is it within the allowed range
var myDate = value.split("/");
var subDay = myDate[0];
var subMonth = myDate[1] - 1;
var subYear = myDate[2];
var subDate = new Date(subYear, subMonth, subDay);
// this will "correct" any out of range input
var calcDay = subDate.getDate();
var calcMonth = subDate.getMonth();
var calcYear = subDate.getFullYear();
// this checks to see if any of the submitted input was out of range
// comment this out to ignore the discrepancy if you want to set a "corrected" value below
if (calcDay != subDay || calcMonth != subMonth || calcYear != subYear) {
$.validator.messages.dob = "Invalid date";
result = false;
}
if (result) {
var currDate = new Date();
var currYear = currDate.getFullYear();
var currMonth = currDate.getMonth();
var currDay = currDate.getDate();
var age = currYear - subYear;
if (subMonth > currMonth) {
age = age - 1; // next birthday not yet reached
} else if (subMonth == currMonth && currDay < subDay) {
age = age - 1;
}
if (ageMin != undefined) {
if (age < ageMin) {
$.validator.messages.dob = "Min 18 years old";
result = false;
}
}
if (ageMax != undefined) {
if (age > ageMax) {
$.validator.messages.dob = "Invalid date";
result = false;
}
}
}
return result;
},
"Please enter a date in the format DD/MM/YYYY");
$("#form").validate({
rules: {
dateBirth: {
required: true,
dob: true
},
month: {
required: true,
dob: true
},
dobDay: {
required: true,
range: [1, 31]
},
dobMonth: {
required: true,
range: [1, 12]
},
dobYear: {
required: true
}
}
});
});
答案 0 :(得分:1)
您可以使用:
$(".bday").on('keyup',function(){
var bday = $('[name="dobDay"]').val()+"/"+$('[name="dobMonth"]').val()+"/"+$('[name="dobYear"]').val();
$("#dateBirth").val(bday );
});