954,178 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Jquery validator - rules with 2 conditions

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:

if ($("input[name='package']").val() != "99" && $("input[name='nameserver_setup']:checked").val() === "SET") {
$(".yearly_ns").each(function() {
$(this).rules("add", { required: true
}); }); }


I have used this code which worked but I am not sure how to go about it with 2 conditions:

$(".1to3year").each(function() {
  $(this).rules("add", { 
        required: function(){
		switch($("#package").val()){
        case "1year" :
        case "2year" :
        case "3year" : return true;
        default: return false;
                } } }); });


Am I not setting the if and && statement up right... Any help appreciated, thanks.

cjay175
Light Poster
49 posts since May 2009
Reputation Points: 10
Solved Threads: 2
 

You are currently doing:

if( conditions )
{
   ...
   required: true
   ...
}

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:

$(".yearly_ns").each(function() {
		$(this).rules(	"add"
						,{	required: function(){
									return (''+$("input[name='package']").val() != '99' && $("input[name='nameserver_setup']:checked").val() == "SET") ;
									} 
						}
					); 
	});
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 

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:

$(".yearly_ns").each(function() {
$(this).rules( "add"
,{ required: function(){
return (($("#package option:selected").val() != '99' || 'promo99') && ($("input[name='nameserver_setup']:checked").val() == "SET")) ;
   } } ); });
!= '99' || 'promo99'

seems to break the code, because when I use just!= '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.

cjay175
Light Poster
49 posts since May 2009
Reputation Points: 10
Solved Threads: 2
 

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.

$("#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,

cjay175
Light Poster
49 posts since May 2009
Reputation Points: 10
Solved Threads: 2
 

you need to EXPLICITLY compare 'promo99' against something as well:

($("#package option:selected").val() != '99' || $("#package option:selected").val()!='promo99')
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You