• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1863
  • Last Modified:

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{''}
                  }
                  
      }
});
0
Panos
Asked:
Panos
  • 5
  • 5
1 Solution
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now