jQuery validate across multiple fields -
i have 3 input fields ask number of people, , of them how many adults , how many children. trying use jquery validate plugin add custom method children + adults = number of people
this validation call
$("#form2").validate({ errorelement: "span", rules: { attendees: { required: true, digits: true }, adults: { required: true, digits: true }, children: { required: true, digits: true } }, messages: { attendees: "enter number of persons (including yourself)", adults: "enter number of adults", children: "enter number of childern" } });
i looked @ group feature , addmetod of validate plugin seems crafted 1 element in mind. ideas ?
follow the documentation creating custom method/rule. here, call totalcheck
, can name wish.
$.validator.addmethod('totalcheck', function(value, element, params) { var field_1 = $('input[name="' + params[0] + '"]').val(), field_2 = $('input[name="' + params[1] + '"]').val(); return parseint(value) === parseint(field_1) + parseint(field_2); }, "enter number of persons (including yourself)");
it's implemented other rule. 2 parameters name
attributes of 2 other fields want check against. since new rule says field must contain sum of 2 other fields, required
, digits
rules redundant , can removed.
$("#form2").validate({ errorelement: "span", rules: { attendees: { totalcheck: ['adults', 'children'] // <-- custom rule }, adults: { required: true, digits: true }, children: { required: true, digits: true } }, messages: { adults: "enter number of adults", children: "enter number of childern" } });
working demo: http://jsfiddle.net/hbxl6/
Comments
Post a Comment