Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  120] [ 1]  / answers: 1 / hits: 18008  / 13 Years ago, mon, june 13, 2011, 12:00:00
function Validator(formIsValid) {
if(this.formIsValid) {
alert('Form is valid!');
}
else {
alert('Form is invalid...');
}
}
Validator.prototype = { // Notice the .prototype here, it's important!

formIsValid: true,

enforceTextFieldMinLength: function(field, minLength) {
if (!field.value || field.value.length < minLength) {
this.formIsValid = false;
}
},

enforceLabelHasText: function(label) {
if (!label.text) {
this.formIsValid = false;
}
}
}
//var val = new Validator();


The above is my Val.js. This is how i am using in my otherFile.js



AddPatient.Firstname = FirstNameValue || Validator.enforceLabelHasText(FirstName);  


I get an error saying cannot find function enforceLabelHasText in Object function Validator(formIsValid)


More From » javascript

 Answers
16

You can't put expressions in an object definition. If you want code to be executed after an object instance is created, you should use:



function Validator() {
if(this.formIsValid) {
alert('Form is valid!');
}
else {
alert('Form is invalid...');
}
}
Validator.prototype = { // Notice the .prototype here, it's important!

formIsValid: true,

enforceTextFieldMinLength: function(field, minLength) {
if (!field.value || field.value.length < minLength) {
this.formIsValid = false;
}
},

enforceLabelHasText: function(label) {
if (!label.text) {
this.formIsValid = false;
}
}
}
var a = new Validator();


This is a dummy solution; you will want to add arguments to the Validator() function, to initialize formIsValid and the other values. I suggest you should read the MDC's description on prototypes.



EDIT: If you went with the prototype solution, you need to call val.enforceLabelHasText(FirstName), after making val a global variable (either by omitting the var or by using var window.val = new Validator()).


[#91746] Friday, June 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
davisbrandtm

Total Points: 387
Total Questions: 99
Total Answers: 106

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;