views:

59

answers:

3

Hi I have the following function, I need to make it return false only if one of two other checkboxes are checked.

$.validator.addMethod(
"ReutersNA",
function(value, element) {
    var selectedCountry = $("#Country").val();
    var NorthAmerica = new Array("USA","CAN","MEX");
    if($.inArray(selectedCountry,NorthAmerica) > -1) {
    return true;
    } else return false;
    }, "Cannot select Reuters News outside of North America."
);

I need to add if($("#IQBAS, #IQPRE").is(":checked") && the above function = return true    
+1  A: 

You mean something like this?

$.validator.addMethod(   
"ReutersNA",   
function(value, element) {   
    var selectedCountry = $("#Country").val();   
    var NorthAmerica = new Array("USA","CAN","MEX"); 
     return ($("#IQBAS, #IQPRE").is(":checked") && $.inArray(selectedCountry,NorthAmerica) > -1);
}, 
"Cannot select Reuters News outside of North America."   
);  
RightSaidFred
@ RightSaidFred This still returns the "Cannot..." if you don't select #IQBAS and or #IQPRE. I need it to return the msg only if you DO check IQBAS and or IQPRE
Dirty Bird Design
A: 

let us try to be a little generic...

$.validator.addMethod(   
"ReutersNA",   
function(value, element, params) {   
    var selectedCountry = value;   
    var NorthAmerica = new Array("USA","CAN","MEX"); 
     return (params && $.inArray(selectedCountry,NorthAmerica) > -1);
}, 
"Cannot select Reuters News outside of North America."   
);

then you could use it like

 rules: {
     Country :  {
         ReutersNA : $("#IQBAS, #IQPRE").is(":checked")
     }
 }
Reigel
I see how this works, validating the rule against the param's being checked....Unfortunately it returns an error no matter what is checked
Dirty Bird Design
...what error??
Reigel
A: 

Your question was a little unclear. I think you mean it should return false (show the message) if it's checked and the element is not in the array.

$.validator.addMethod(   
"ReutersNA",   
function(value, element) {   
    var selectedCountry = $("#Country").val();   
    var NorthAmerica = new Array("USA","CAN","MEX");
    // Valid (true) if neither checked, or element is found in array.  
    return !$("#IQBAS, #IQPRE").is(":checked") || $.inArray(selectedCountry,NorthAmerica) > -1);
}, 
"Cannot select Reuters News outside of North America."   
);  
Matthew Flaschen
I get this error missing ; before statement[Break on this error] || $.inArray(selectedCountry,NorthAmerica) > -1);\n
Dirty Bird Design
@Dirty, put the two conditions on the same line (I just tweaked the answer to do that).
Matthew Flaschen