0

I've put together a little javascript file which uses a clean framework, here's how it's formatted:

var functionLibrary = {

    validateFields:function(inputArray, result) {
        $.each(inputArray, function(index, listItem) {
            if (!$.trim($("#" + listItem).val()).length) {
                return "empty";
            }   
        });
    }

}

Here's me passing data to such function and getting an undefined when validating the response:

var Validation = functionLibrary.validateFields(['amount', 'ppemail']);
console.log(Validation);

Why does such function show undefined in the console log when it should be returning empty ?

2
  • the validateFields is not returning anything... the each callback is returning the value Commented Sep 2, 2014 at 12:31
  • The each is not returning for validateFields. Try to put a console.log before the end of validateFields to check if the function didn't return "empty" yet. Commented Sep 2, 2014 at 12:33

1 Answer 1

2

You are not returning anything from the validateFields method...

var functionLibrary = {

    validateFields: function (inputArray, result) {
        var value;
        $.each(inputArray, function (index, listItem) {
            if (!$.trim($("#" + listItem).val()).length) {
                //set the validity status
                value = "empty";
                //stop further execution of the loop
                return false;
            }
        });
        //return the value from validateFields
        return value;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.