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 ?

Curtis Crewe
  • 4,126
  • 5
  • 26
  • 31
  • the `validateFields` is not returning anything... the each callback is returning the value – Arun P Johny Sep 02 '14 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. – bzeaman Sep 02 '14 at 12:33

1 Answers1

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;
    }

}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531