So far I got:

$(function(){
   $.validator.addMethod("compare", function(value, element, param){
      Date.format = 'mmm. dd, yyyy';
      sdate = Date.fromString($(param[0]).val());
      edate = Date.fromString($(param[1]).val());

      return (sdate.getTime() <= edate.getTime()) || this.optional
(element);
   }, "End date must be later than the Start date")

   var rules = {};
   $(".repeatingformfield").each(function(){
      $(this).find(":text").each(function(n){
         if (n == 0)
            rule_name = this.name;
         if (n == 1)
            start_date = this.id;
         if (n == 2)
            end_date = this.id;
      });
      rules[rule_name] = {
                     compare: ["#" + start_date, "#" + end_date]
      }
   });

   $(".tableform").validate({debug: true, options: rules});
});

But I guess my logic is not that great since the compare method
doesn't seem to be called. Perhaps the method definition does not
match the way I'm calling it but I'm not able to see that on firebug.

Any suggestions?

Diego.


On Dec 18, 4:10 pm, dfiguero <dfigu...@gmail.com> wrote:
> Thanks Jörn I'll give it a try!
>
> On Dec 18, 12:17 pm, "Jörn Zaefferer" <joern.zaeffe...@googlemail.com>
> wrote:
>
> > You could generate the rules object.
>
> > var rules = {};
> > $(":input[name*=date-start"]).each(function() {
> >   rules[this.name] = { ... }});
>
> > $("...").validate({options:rules});
>
> > Jörn
>
> > On Thu, Dec 18, 2008 at 3:01 PM,dfiguero<dfigu...@gmail.com> wrote:
>
> > > 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
>
>

Reply via email to