0

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.

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by weblike
0

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") ;
									} 
						}
					); 
	});
0

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.

0

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,

0

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

($("#package option:selected").val() != '99' || $("#package option:selected").val()!='promo99')
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.