Hello im using jquery validation plugin to validate my form. I want to add one more rule to the validation, but cant get it to work. Input value should be less or equal previous element value.
It is possible to do it ?
Here is the code -
<input type='text' value='<?php echo $count; ?>' class='input_in_stock' readonly="readonly" />
<input type='text' name='amount[]' id='<?php echo rand(0,99).$id; ?>' size='1' value='<?php echo $count; ?>' class='required input_count' />
$(document).ready(function(){
$.validator.addMethod('positiveint', function (value) {
return (value > 0) && (value != 0) && (value == parseInt(value, 10));
}, 'Enter a positive integer value.');
$.validator.addMethod('notmorethan', function (value) {
return (value <= $(this).prev().val());
}, 'abc.');
$("#cartform").validate({
rules: {
"amount[]": {
required: true,
positiveint: true,
notmorethan: true
}
},
errorPlacement: function(error, element) {
error.appendTo(element.next());
}
});
});
p.s. I used this jquery validation plugin fix to make it work with names like this[] http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements
EDIT: found the solution
$.validator.addMethod('notmorethan', function (value, element) {
return (value <= $(element).prev().val() );
}, 'abc');