1

I am using jQuery validation plugin 'jquery.validation.js' to validate date data type in 'date_of_birth' field

I want to display a custom message above the "date_of_birth" filed as 'Age should be minimum of 16' .

So I have written the following customised validation code :

{literal}
jQuery.validator.addMethod("dob", function (value, element) {
var startDate = $('#date_of_birth').val();
var finDate = 01/03/2012
var age=Date.parse(finDate)-Date.parse(startDate);
if (age >= 16){ 
    return false;
      }
      else
     {
    return true;
     }
 }, "Age should be minimum of 16");
{/literal}

custom validation rules:

rules: {        
date_of_birth: {
dob: true
}
}

But the sad part is that it's not working.

Please help me out...

Thanks in advance.

4

2 回答 2

2
$.validator.addMethod("dob", function() {
    var minDate  = new Date();
    minDate.setFullYear(minDate.getFullYear() - 16);

    var dob =  $('#date_of_birth').val();
    if( dob  <= minDate)
        return true;
    return false;
}, "Please specify a correct DOB:");

和:

 rules: {
    date_of_birth: { required: true, dob: "date_of_birth" },
},
于 2012-06-05T03:30:12.020 回答
1

我认为jQuery EasyDate正是您正在寻找的。

于 2012-06-05T02:52:37.690 回答