943,169 Members | Top Members by Rank

Ad:
Aug 29th, 2010
0

Jquery validator - rules with 2 conditions

Expand Post »
Hi, Just looking for a quick hand in a question.

I am using jquery validator for a form and some dynamic fields

I am trying to have some fields required only if 2 conditions have been might, a select box selected and a radio button checked... Here is the code I am trying to get work:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. if ($("input[name='package']").val() != "99" && $("input[name='nameserver_setup']:checked").val() === "SET") {
  2. $(".yearly_ns").each(function() {
  3. $(this).rules("add", { required: true
  4. }); }); }

I have used this code which worked but I am not sure how to go about it with 2 conditions:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $(".1to3year").each(function() {
  2. $(this).rules("add", {
  3. required: function(){
  4. switch($("#package").val()){
  5. case "1year" :
  6. case "2year" :
  7. case "3year" : return true;
  8. default: return false;
  9. } } }); });

Am I not setting the if and && statement up right... Any help appreciated, thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
cjay175 is offline Offline
49 posts
since May 2009
Aug 29th, 2010
0
Re: Jquery validator - rules with 2 conditions
You are currently doing:
javascript Syntax (Toggle Plain Text)
  1. if( conditions )
  2. {
  3. ...
  4. required: true
  5. ...
  6. }
If you look closer at your working example, your required is NOT true|false, it is a reference to a function that returns true or false. So, instead of what you have (and is not working) put your conditions in an anonymous function and assign that anonymous function to required:

javascript Syntax (Toggle Plain Text)
  1. $(".yearly_ns").each(function() {
  2. $(this).rules( "add"
  3. ,{ required: function(){
  4. return (''+$("input[name='package']").val() != '99' && $("input[name='nameserver_setup']:checked").val() == "SET") ;
  5. }
  6. }
  7. );
  8. });
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Aug 29th, 2010
0
Re: Jquery validator - rules with 2 conditions
Thanks a lot,
that makes sense, I have got it working. Just had one more quick question though.

I tried to put an or (||) statement inside the first condition but it isnt working, heres my code now:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $(".yearly_ns").each(function() {
  2. $(this).rules( "add"
  3. ,{ required: function(){
  4. return (($("#package option:selected").val() != '99' || 'promo99') && ($("input[name='nameserver_setup']:checked").val() == "SET")) ;
  5. } } ); });

Quote ...
!= '99' || 'promo99'
seems to break the code, because when I use just
Quote ...
!= '99'
it works fine.

Should I use something with str.search() to search for 99 or am I doing the or statement wrong.

Thanks again for the help I really appreciate it.
Reputation Points: 10
Solved Threads: 2
Light Poster
cjay175 is offline Offline
49 posts
since May 2009
Aug 29th, 2010
0
Re: Jquery validator - rules with 2 conditions
Ok so I found a workaround, not sure why the or (||) statement wasnt working but I decided to put a class instead of getting the value.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $("#package option:selected").attr("class") != '99'
Then I just put the class name 99 on the values I wanted to have the condition.

ANyways, thanks a lot for the help,
Reputation Points: 10
Solved Threads: 2
Light Poster
cjay175 is offline Offline
49 posts
since May 2009
Aug 29th, 2010
0
Re: Jquery validator - rules with 2 conditions
you need to EXPLICITLY compare 'promo99' against something as well:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. ($("#package option:selected").val() != '99' || $("#package option:selected").val()!='promo99')
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: javascript web music player control
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Google chrome File upload problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC