i am trying to build a Form Validator. when i am trying to access the rules object, i am getting a Uncaught ReferenceError. What am i doing wrong here?
class Validator {
constructor(formID, rules) {
this.form = document.querySelector(formID);
this.rules = rules;
this.init();
}
init() {
form.addEventListener('submit', this.validate);
}
validate(e) {
e.preventDefault();
let formElements = form.elements;
let toCheck = [];
for (let elem of formElements) {
if(elem.type != 'submit') {
toCheck.push(elem);
}
}
toCheck.forEach(function(elem) {
let element = elem.type;
console.log(rules["name"]); //Uncaught ReferenceError: rules is not defined
});
}
}
const val = new Validator('#form', { name: "checked by namechecker", email: "checked by mail checker"});
EDIT: Sorry, i am not seeing the dublicate, as for a example i could console.log(form) in the same spot without an error.
EDIT 2: After some help in the comments i do understand that it is a duplicate. Sorry.