I have a div in jsp as follows:
<input type="text" id="year" placeholder=" Year" class="year" name= "year" value=""/>
I want to calculate the difference between current year and entered year. I have to show the error if the value does not lie within some range. This is my custome jquery validator function.
jQuery.validator.addMethod('validAge', function (value, element, params) {
alert("inside add method");
var currentYear = (new Date).getFullYear();
var range = currentYear - $element.val();;
if(range >10 && range< 70)
{
return true;
}
return false;
}),
But it is showing error at element.val I have defined the rule as
$('#signup_form').validate({ // initialize the plugin
rules: {
year: {
validage: true,
},
},
messages: {
year: "Invalid year",
},
},
});
In jquery addMethod element (the element to be validated) and value (the current value of the validated element)
jQuery.validator.addMethod("validAge", function (value, element, params) {
alert("inside add method");
var currentYear = (new Date).getFullYear();
var range = currentYear - value;
return this.optional(element) || (range >10 && range< 70)
},'Enter range in between 10 to 70');
Remove commas where not needed and also see case sensitiveness.
var validator = $('#form').validate({
rules: {
year: {
validAge: true
}
},
messages: {
year: "Invalid year"
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With