Reputation: 7190
Gentlepeeps,
I'm new with Jquery. Apologies in advance for the basic question.
I'm trying to bring in Jquery to our struts/jsp environment. Existing code is riddled with pure javascript.
I'm writing a validation rule using JQuery's validation plugin like so:
$("#advanceValidateform").validate({
rules: {
advanceAmt1: { min: 0, max: 23 },
advanceAmt2: { min: 0, max: 24 },
advanceAmt3: { min: 0, max: 26 }
}
});
The problem is:
A solution would be to parameterize the whole thing, have a validation rule for advanceAmt with parameters min, max and call that for each input element containing the "class" .advanceAmt .
essentially I require the jquery validation to :
But i'm a little lost how to do the same with jquery. Is this possible? If not any suggestions or better alternatives?
Thanks in advance. K
Upvotes: 1
Views: 823
Reputation: 103145
On your JSP page you can use a JSTL iterator to go through the collection of validation rules and generate each one. This does not affect the javascript code because the JSTL evaluates on the server and the resulting JavaScript evaluates on the client.
For example, suppose you have a collection of validation rules called validations that was created by your action.
$("#advanceValidateform").validate({
rules: {
<c:forEach var="v" items="${validations}" varStatus="loop">
advanceAmt${v.id}: { min: ${v.min}, max: ${v.max} }${!loop.last ? ',' : ''}
</c:forEach>
}
});
Some assumptions - Your collection of validation rules contain three properties id, min and max. The if statement was necessary to add a comma between the rules but not after the last one.
Hope this helps a bit.
Upvotes: 2