I am exploring the concept of converting this code into a namespace to enhance clarity and prevent global namespace pollution. As someone relatively new to this, I would really appreciate some guidance or examples on how to achieve the conversion to 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 understand that this object-oriented approach to validation may seem excessive!) To trigger the runValidate function, I use a button in a form like this: "runValidate(this.form)".