Jörn thanks for all your help, I read some more of your comments on
the plugin page and I finally solved my problem with the code below
(just in case anyone runs into the same issue):

   $.validator.addMethod("start", function(value, element, param){
      Date.format = 'mmm. dd, yyyy';
      today = new Date();
      today.zeroTime();
      dateval = $("#" + param[0]).val();
      sdate = Date.fromString(dateval);

      return (sdate.getTime() >= today.getTime()) || this.optional
(element);
   }, "Your Start date must be set to today or later")

   $(".tableform").submit(function(form){
       $(".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)rules
                    end_date = this.id;
              });
              rules[rule_name] = {
                     start: [start_date]
              }
       })
   }).validate({
        rules: rules,
        errorPlacement: function(error, element){
             element.parent().parent().next().next().next().replaceWith
(error)
        }
        //debug: true
   });

On Jan 5, 9:25 am, dfiguero <dfigu...@gmail.com> wrote:
> Thanks for your help Jörn. I finally figured out what my problem was.
> The page doesn't load all the input elements in the form until the
> user manually selects a year. In other words the rules get empty
> values because there are no matching rules at the point where $
> (function()...) is call.
>
> Now I just need to find a way to create the rules before submitting!
>
> Thanks,
>
> Diego.
>
> On Dec 23, 1:01 pm, "Jörn Zaefferer" <joern.zaeffe...@googlemail.com>
> wrote:
>
> > Replace this:
> > $(".tableform").validate({debug: true, options: rules});
> > with this:
> > $(".tableform").validate({debug: true, rules: rules});
>
> > Jörn
>
> > On Fri, Dec 19, 2008 at 4:00 PM, dfiguero <dfigu...@gmail.com> wrote:
>
> > > 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