[jQuery] Dynamic form validation on different names possible?
Hi,
I'm trying to validate a form with a dynamic date fields. Something
like:
<tr id="period1" class="someclass">
<td>
<input type="text" name="date-1-start" id="date_1-start"
value="Mar. 29, 2008" />
<input type="text" name="date-1-end" id="date_1-end" value="Mar.
29, 2008" />
</td>
</tr>
<tr id="period2" class="someclass">
<td>
<input type="text" name="date-2-start" id="date_2-start"
value="Jun. 02, 2010" />
<input type="text" name="date-2-end" id="date_2-end" value="Jun.
02, 2010" />
</td>
</tr>
...
<tr id="period3" class="someclass">
<td>
<input type="text" name="date-n-start" id="date_n-start"
value="aaa. 99, 9999" />
<input type="text" name="date-n-end" id="date_n-end" value="aaa.
99, 9999" />
</td>
</tr>
I got my validation rules working but only if I hardcode each of the
input's names:
$("form").validate({
rules: {
"date-1-start": compare ["#date_1-start","#date_1-end"],
"date-2-start": compare ["#date_2-start","#date_2-end"],
...
"date-n-start": compare ["#date_n-start","#date_n-end"]
}
});
Would there be a way to dynamically generate the rules? Something
like:
$("form").validate({
rules: {
$(":input[name*=date-start"]): compare [$(":input[name*=date-
start"]),$(":input[name*=date-end"])],
}
});
I know I could perhaps do this by adding classes to the input fields
but I would prefer avoiding that option.
Thanks