+ 1

How can I validate email ??

I get error when I use regex even if format Is correct, what can I do?? https://code.sololearn.com/WwS85Z1XaK3C/?ref=app

31st Aug 2023, 6:22 AM
Indiphile Menziwa
Indiphile Menziwa - avatar
2 Answers
+ 1
However, there is a small bug in how you get the email value from the input field. You must get the value inside the "click" event handler because in the current code, the email value is retrieved once on page load and is not updated every time the button is clicked. let submit = document.querySelector("#submit"); let validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; submit.addEventListener("click", () => { let email = document.querySelector("#email").value; if (email.match(validRegex)) { console.log("true"); } else { console.log("false"); } });
31st Aug 2023, 11:54 AM
Kate
0
I see hence why you put the email element inside the eventListener so the every time you click the button it renews or atleast check the new value...
31st Aug 2023, 12:30 PM
Indiphile Menziwa
Indiphile Menziwa - avatar