GautamD31
GautamD31

Reputation: 28763

Validation for either of the fields are non-empty

Suppose I have two text fields and I need to validate for either of the fields are non-empty.

I have tried like

"vImage": {
    "myfunction_valid": function(){
                 if(('#vimg1').val()) return true;
                 else if($('#vimg2').val()) return true; 
                 else return false; 
          }
        }

But it is not working. Can anyone suggest me the better way?

Upvotes: 0

Views: 57

Answers (1)

nnnnnn
nnnnnn

Reputation: 150040

Presumably the code shown is part of an object literal, and there could be other problems in the code you haven't shown, but in what you have shown you've got a syntax error on the line with the if:

if(('#vimg1').val()) return true;

Should be:

if($('#vimg1').val()) return true;

For further help you will need to show us more of your code. How do you call that function? What is the rest of the object that vImage belongs to?

"Can anyone suggest me the better way"

Well you could simplify the function to this:

"vImage": {
    "myfunction_valid": function(){
          return $('#vimg1').val() != "" || $('#vimg2').val() != "";
     }
 }

Upvotes: 1

Related Questions