So I want to build a form validation class/object in javascript. The way I see it working would be something like this:
var form=new Validation();
form.addField("name","Your name","required");
form.addField("email","Email Address","is_email");
.........
form.validate();
I was thinking that the validation
class would be defined something like this?
validation
{
var fields=new Array();
var labels=new Array();
var rules=new Array();
var count=0;
function addField(field,label,rule)
{
this.fields[count]=field;
this.labels[count]=label;
this.rules[count]=rule;
this.count=count+1;
}
function validate()
{
var valid;
for (x=0; x< count; x++)
{
valid=false;
switch (this.rules[x])
{
case 'required':
valid=this.validate_required(this.fields[x]);
break;
case 'email':
valid=this.validate_email(this.fields[x]);
break;
}
if (! valid)
this.addError(this.fields[x],this.rules[x],this.labels[x]);
}
if (this.hasErrors())
return false;
else
return true;
}
..........
}
I'm aware that this probably isn't possible as it is right now. My question is, what can I do to so the first block of code (which creates a new instance of Validation
and adds the rules to it) would work?
Thanks in advance.