I have a function called Validation which contains three functions. Now when I want to call one other function from one function inside Validation i.e. on line 13. Then I get this error
Uncaught TypeError: Object [object Object] has no method 'validateAddress'
Here is my code
var Validation = function () {
var inputs, field, errors = [], self = this,
emptyFieldsValidation = function () {
$('#form input').each(function (i, el) {
inputs = $(this);
if ( inputs.val() == '' ) {
inputs.css('border', '1px solid red');
return errors.push('emptyFields');
} else {
inputs.css('border', '1px solid #ccc');
if (inputs.hasClass('from')) {
if (!self.validateAddress(inputs.val()))
errors.push('invalidFromAddress');
}
if (inputs.hasClass('to')) {
if (!self.validateAddress(inputs.val()))
errors.push('invalidToAddress');
}
if (inputs.hasClass('time')) {
if (!self.validateForNumber(inputs.val()))
errors.push('invalidTime');
}
}
});
return !!errors.length;
},
validateAddress = function (val) {
var streetregex = /^[\w+\s]+\d+,\s*[\s\w]+$/;
if (streetregex.test(val)) return true;
else return false;
},
validateForNumber = function (val) {
if (!isNaN(val)) return true;
else return false;
};
return {
emptyFieldsValidation: emptyFieldsValidation
};
}
Replace
self.validateAddresswith justvalidateAddressinemptyFieldsValidationYou can read more about nested functions and closures here.