You problem is that inside your formSubmit
function the this
value isn't what you think.
A way to fix that would be to bind it to your classe's this
.
You could either do that when you call the function, of you can just add it to your constructor.
Something like this:
class Form {
constructor(classForm, name, email, phone, text) {
this.form = document.querySelector(`.contactForm`);
this.name = document.querySelector(`.contactForm [name=${name}]`);
this.email = document.querySelector(`.contactForm [name=${email}]`);
this.phone = document.querySelector(`.contactForm [name=${phone}]`);
this.text = document.querySelector(`.contactForm [name=${text}]`);
this.submit = document.querySelector(`.contactForm [name=submit]`);
this.formSubmit = this.formSubmit.bind(this);
//this.form.addEventListener('submit', this.formSubmit);
}
checkPhone() {
return this.phone.value.length >= 12;
}
formSubmit(el) {
console.log(this.checkPhone())
el.preventDefault();
console.log('not')
}
}
const formContact = new Form("contactForm", "nameUser", "emailUser", "phoneUser", "textUser");
formContact.form.addEventListener("submit", formContact.formSubmit);
<form action="#" class="contactForm">
<input type="text" name="nameUser" />
<input type="email" name="emailUser" />
<input type="text" name="phoneUser" />
<input type="text" name="textUser" />
<input type="submit" name="submit" value="submit" />
</form>
idea 1
You can also see at the end of the constructor i have added a extra this.form.addEventListener('submit', this.formSubmit);
(which is commented out).
If you really want a class to take care of the form, then why not let the class listen to the form submit. This means you would not need to call it from outside the class.
(you could replace your formContact.form.addEventListener("submit", formContact.formSubmit);
with the this.form.addEventListener('submit', this.formSubmit);
inside the class constructor)
Idea 2
Something else you could try would be to change your formSubmit
into an arrow function:
formSubmit = (el} => {
...
}
Arrow functions don't have the same this
as normal function
Info
Here is some useful information about the bind
on MDN
PS:
Also, you forgot the "textName" parameter on your new Form(...)