Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Montana, USA
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    jQuery validation rules, conditional

    My form contains a label with 2 checkbox inputs (id=name, name = memtype), one of which is 'guide'. I have memtype required in validation rules with an appropriate message for failing to check a box.

    I also have another text input (id=sponout, name=sponout) that MUST be filled in IF memtype = 'guide'. How may I set a rule for this 'if condition' so that if a user checks memtype 'guide', but don't enter text in sponout, they receive an error message?

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,640
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    Are you using a validation plugin?

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Montana, USA
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, the jquery validate plugin.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    The docs provide the following example to create a dependency:
    Code:
    $(".selector").validate({
       rules: {
         contact: {
           required: true,
           email: {
             depends: function(element) {
               return $("#contactform_email:checked")
             }
           }
         }
       }
    })
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    robincham (09-21-2012)

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Montana, USA
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Andrew:

    Thanx. Could have searched for days in jquery validation rules for that bit.

  • #6
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Montana, USA
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I track the example suggested, but it relies on a single checkbox (email). Since I have two checkboxes (outfitter, guide) $_POSTed to $memtype, I'm lost in the logic of implementing as per the example. I simply don't see how to handle the memtype checkbox choice 'guide' as 'checked' in

    sponout: {
    required:true,
    memtype: {
    depends: function(element) {
    return $("#contactform_memtype:checked")
    }
    }
    },

  • #7
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Montana, USA
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Answer is much simpler than I thought:

    sponout: {
    required:"#guide:checked"
    }


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •