Using if statements inside jquery validation rules

Hello experts.
I want tou use if statements inside jquery validation rules like the following example. If the field exists in an array where i load all the required fields  it should be required true else required false + selectNone true
Take a look at my code that is not working:
jQuery.validator.addMethod("selectNone", function(a) {return true});
var requiredfield = 'required field';
var arrayrequired = ['select1','select2'];// example array , his data will be created dynamically
$("#Form1").validate({
      rules: {
             select1: function(e){
                   if ( $.inArray(select1, arrayrequired) != -1){required: true}
                   else{required:false,selectNone:true}
                   },
             select2: function(e){
                   if ( $.inArray(select2, arrayrequired) != -1){required: true}
                   else{required:false,selectNone:true}
                   },
      },
      messages:{
            select1: function(e){
                  if ( $.inArray(select1, arrayrequired) != -1){requiredfield}
                  else{''}
                  },
            select2: function(e){
                  if ( $.inArray(select2, arrayrequired) != -1){requiredfield}
                  else{''}
                  }
                  
      }
});
LVL 2
PanosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
I don't have a lot of experience with the jQuery validator but it seems like you could just loop through the array of selects and add the rules (untested code example).
jQuery.validator.addMethod("selectNone", function(a) {return true});
var requiredfield = 'required field';
var arrayrequired = ['select1','select2'];// example array , his data will be created dynamically
var validator = $("#Form1").validate();
for (var i=0; i<arrayrequired.length; i++) {
   validator.element(arrayrequired[i]).rules("add", {
	   required: true,
	   messages: {
		   required: requiredField
	   }
   });
}

Open in new window

0
PanosAuthor Commented:
Hi Tom Beck
Thank you for your code. I really prefer a solution using the jquery validation plug in because i don't want to rewrite all the code and because i  understand it so that i can use it for more complicated coding.
0
PanosAuthor Commented:
Hi again.
I have a working code but i don't know if this would be the best one:
rules: {
    select1: {required:( $.inArray('select1', arrayrequired) != -1),selectNone:($.inArray('select1', arrayrequired) == -1)},
.....
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Tom BeckCommented:
??
My code is for the jQuery validate plugin.

The point of using a loop is that it would only add rules and messages for the selects that are dynamically created. Eliminates a lot of repetition.
0
Tom BeckCommented:
Here's a demo I created on jsfiddle. The untested code I posted earlier didn't quite work but the concept was valid.

http://jsfiddle.net/tommyBoy/4ychgxzt/
0
PanosAuthor Commented:
Hi Tom Beck
You are right. It is a jquery approach and i understand now what you ment with the loop function. The question was more how to use the if statement. Can you expand your code for selects that are not required using the selectNone function? (required:false,selectNone:true).
I have than to look further what to do with textfields a.s.o. It will be complicated i think.
0
Tom BeckCommented:
Here's an update of my jsfiddle where I've added two more selects. For the new selects the name does not start with "select" so they are not included in the array and thus not required for form submission. There's no validation error if no selection is made on those two. Is that not the same effect as your selectNone method? Using if statements and calling inArray for every rule seems bloated and unnecessary. But I may be misunderstanding your requirements.

http://jsfiddle.net/4ychgxzt/1/

I can try to study the use of if statements in the rules a little later but I've already learned more about validate.js than I've ever cared to know :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PanosAuthor Commented:
Hi Tom Beck
The idea of if statements was wrong. Using the array idea is much better.
I created a fiddle using jquery and 'each' function (the array function of jquery) and using classes for validation.
Here is the code:
https://jsfiddle.net/panosms/qzhutoz3/2/

Thank you very much for your help!!!!
0
PanosAuthor Commented:
Thank you very much
regards
panos
0
Tom BeckCommented:
Looks good, very efficient!
Line 3 is unnecessary and in reality assigns $currentElem to the document itself since $(this) is outside of any function.

Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.