Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-15-2012, 09:08 PM   PM User | #1
robincham
New to the CF scene

 
Join Date: Sep 2012
Location: Montana, USA
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
robincham is an unknown quantity at this point
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?
robincham is offline   Reply With Quote
Old 09-16-2012, 12:02 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,602
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Are you using a validation plugin?
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 09-17-2012, 09:00 PM   PM User | #3
robincham
New to the CF scene

 
Join Date: Sep 2012
Location: Montana, USA
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
robincham is an unknown quantity at this point
Yes, the jquery validate plugin.
robincham is offline   Reply With Quote
Old 09-17-2012, 10:05 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
robincham (09-21-2012)
Old 09-21-2012, 03:27 PM   PM User | #5
robincham
New to the CF scene

 
Join Date: Sep 2012
Location: Montana, USA
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
robincham is an unknown quantity at this point
Andrew:

Thanx. Could have searched for days in jquery validation rules for that bit.
robincham is offline   Reply With Quote
Old 09-24-2012, 07:59 PM   PM User | #6
robincham
New to the CF scene

 
Join Date: Sep 2012
Location: Montana, USA
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
robincham is an unknown quantity at this point
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")
}
}
},
robincham is offline   Reply With Quote
Old 10-07-2012, 03:59 PM   PM User | #7
robincham
New to the CF scene

 
Join Date: Sep 2012
Location: Montana, USA
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
robincham is an unknown quantity at this point
Answer is much simpler than I thought:

sponout: {
required:"#guide:checked"
}
robincham is offline   Reply With Quote
Reply

Bookmarks

Tags
conditional, jquery, validate

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:24 PM.


Advertisement
Log in to turn off these ads.