improve my jquery validation plugin code

Posted by Jared on Stack Overflow See other posts from Stack Overflow or by Jared
Published on 2010-05-20T05:31:47Z Indexed on 2010/05/20 5:40 UTC
Read the original article Hit count: 206

Filed under:
|

Hi All

Just hoping soemone can help me to write better code than I can come up with on my own.

I am using the jquery validation plugin. I have some fields that are mandatory ONLY if certain options are chosen.

The below code works fine. But the thing is, is that that my list of 'OR's is much longer than I've put here. and it needs to be applied not just to 'directorsName' but a whole long list of inputs, selects etc.

My question is.. how can I wrap up the code contained inside the RETURN? (so I dont have to keep repeating my 'OR's. I'm guessign I need a function but I'm unsure of the syntax)

$("#myForm").validate({
 rules: {
  directorsName : { 
   required: function(element) {
   return ( $('#account_for').val() == "Joint" || $('#directors_number').val() == "2" || $('#directors_number').val() == "3" );
   }
  }
 }
});

Thanks in advance

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript