I am trying to convert this code to a namespace to make it cleaner and to avoid polluting the global namespace. I'm fairly new to this, and could use a little guidance or examples how I would convert the following code into a javascript namespace.
function Validator(fields) {
this.fields = fields;
}
Validator.prototype.validate = function(form) {
for(var i = 0, l=this.fields.length; i < l; i++) {
alert(this.fields[i].value);
if (this.fields[i].value == 0) {
alert("The field is empty");
return false;
}
}
}
var validator = new Validator([ "username", "password"]);
function runValidate(form) {
validator.validate(form);
}
(I know this OO approach to validation is excessive!) I call this runValidate from a button in a form like this "runValidate(this.form)".
runValidatefunction really necessary? It saves you only 7 chars.validator.validate(this.form)from the button