Try changing it so it's:

required: function() { return $("[name=billing_addr]:checked").val()
== 0; }

and remove your click binding function.
This makes the radio check done on runtime.

Doing: return billing_address == 0;
will probably only evaluate "billing_address" when the rules are
defined (which is at the beginning of script execution), so it's
probably 0, and your fields were always required, if I'm correct.


Aside from that problem, here's some additional facts you can learn
from. :)
When you write:

$("input.billing_addr_grp").click(function() {
    var billing_address = $("[name=billing_addr]:checked").val();
})

Your variable billing_address will only be considered local, and valid
inside only the scope of that click function because you defined the
variable inside there (using "var"). This means that other code
outside that function would not be able to see the value you're trying
to set.
Instead, you'd want to set a global variable. Do something like:

var billing_address;  // global variable; any code would be able to
access this

$("input.billing_addr_grp").click(function() {
    // this will write the value to the global variable
    billing_address = $("[name=billing_addr]:checked").val();
})



On May 15, 5:55 am, choffman <mountain....@gmail.com> wrote:
> Thanks James I appreciate you replying.  I'm still having some
> trouble, even with your additions.  I changed the checkbox to radio
> buttons.  I added this code snippet to get the value of the checked
> radio. Yes == 1 and No == 0, if the user selects No (0) then the
> billling address fields need to be required. Does this make sense?
>
> $("input.billing_addr_grp").click(function() {
>   var billing_address = $("[name=billing_addr]:checked").val();
>
> })
>
> Then the rules:
> $('#transaction').validate({
>                 rules: {
>                         first: "required",
>                         last: "required",
>                         billing_first: {
>                                 required: function() { return billing_address 
> == 0; }
>                         },
>
>                         billing_last: {
>                                 required: function() { return billing_address 
> == 0; }
>                         }
>
> I still can't get them to only be required when the value of the radio
> button is 0.  Any more thoughts?
> Thanks,
> MD
>
> On May 14, 6:19 pm, James <james.gp....@gmail.com> wrote:
>
> > Something like:
>
> > rules: {
> >      first: "required",
> >      last: "required",
> >      ......
> >      billing_first: {
> >           required: function() { return billing_address == 1; }
> >      },
> >      billing_last: {
> >           required: function() { return billing_address == 1; }
> >      },
> >      billing_first: {
> >           required: function() { return billing_address == 1; }
> >      },
> >      billing_address: {
> >           required: function() { return billing_address == 1; }
> >      },
> >      .....
>
> > },
>
> > On May 14, 10:14 am, choffman <mountain....@gmail.com> wrote:
>
> > > Hi There,
> > > I could use some help here.  I need to place a conditionstatement
> > > (if) in thisformvalidationscript.  For some reason, I just can't
> > > get it to work.  Basically, when a user un-checks a particular box
> > > (billing_addr) additionalformfields slide down, those fields need to
> > > be validated.  Ifthe box is checked the fields roll-up, become
> > > disable and can be skipped.  What's the best way to accomplish this?
>
> > > Thanks,
> > > MD
>
> > > $(document).ready(function() {
> > >         var billing_address = 
> > > $("input[name='billing_addr']:!checked").val();
> > >         $.metadata.setType("validate");
> > >         $('#mysampleform').validate({
> > >                 rules: {
> > >                         first: "required",
> > >                         last: "required",
> > >                         address: "required",
> > >                         city: "required",
> > >                         state: "required",
> > >                         country: "required",
> > >                         zip: "required",
> > >                         membertype: "required",
> > >                         amount: "required",
> > >                         state: "required",
> > >                         mt: "required",
> > >                         email: true,
>
> > >                        if(billing_address == 1) {
> > >                                 alert(billing_address);
> > >                                 billing_first: "required",
> > >                                 billing_last: "required",
> > >                                 billing_address: "required",
> > >                                 billing_city: "required",
> > >                                 billing_state: "required",
> > >                                 billing_zip: "required",
> > >                                 billing_email: true
> > >                         }
> > >                 },
> > >                 messages: {
> > >                         first: "Please enter your first name",
> > >                 }
> > >         });
>
> > > });

Reply via email to