Okay... relative newbie here, probably overreaching myself but here goes. I've created an object in order to help validate a form. The object is called "Field" and looks like this:
var Field = {
mini : {
val : 3,
err : "Wrong length. Your entry needs to be greater than 2 characters",
valid : false,
check : function(target){
var target = event.target || event.srcElement;
if (target.value.length < this.mini.val) {
this.mini.valid = false;
fadeIn("mini",this.mini.err);
} else {
this.mini.valid = true;
fadeOut("mini",this.mini.err);
return null;
}
},
},
max : {
val : 5,
err: "Wrong length. Your entry needs to be less than 30 characters",
valid : false,
check : function(target){
var target = event.target || event.srcElement;
if (target.value.length > this.max.val) {
this.max.valid = false;
fadeIn("max",this.max.err);
} else {
this.max.valid = true;
fadeOut("max",this.max.err);
return null;
}
},
},
regex : {
val : /[^a-zA-Z- ]/,
err : "Letters, hyphens, apostrophes and spaces only please.",
valid : false,
check : function(){
var target = event.target || event.srcElement;
if (target.value.match(this.regex.val)) {
this.regex.valid = false;
fadeIn("regex",this.regex.err);
} else {
this.regex.valid = true;
fadeOut("regex",this.regex.err);
return null;
}
},
},
hintID : ""
}
I get that this in itslf is somewhat clumsy and I'll be looking at ways to sort that further down the line.
I've created several instances of this object as follows:
var firstName = Object.create(Field);
firstName.hintID = fnHint;
firstName.regex.val = /[^a-zA-Z- ]/;
firstName.regex.err = "Letters, hyphens, apostrophes and spaces only please.";
var lastName = Object.create(Field);
lastName.hintID = lnHint;
lastName.regex.val = /[^a-zA-Z- ]/;
lastName.regex.err = "Letters, hyphens, apostrophes and spaces only please.";
var age = Object.create(Field);
age.max.val = 3;
age.max.err = "Wrong length. The age needs to be no more than 3 characters long.";
age.regex.val = /[^0-9]/;
age.regex.err = "Numbers only please.";
age.hintID = agHint;
So far so good.
The problem occurs when I try to access the separate object instances as despite what I call, I always seem to get the last occurence. For example:
console.log(firstName.regex.err);
gives me the value of the last declared instance of the regex.err, before the console.log call. For example, if I place that code after lastName it gives me the value for lastName.regex.err, after age it gives me the value for age.regex.err... I'm probably doing something silly here - it has been know :-) I've checked through it but can't seem to figure it out. Any ideas?
Thanks in advance.
Stef