Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29758

"if" statement for validating a form doesn't work properly

$
0
0

I have this newsletter form with email input generated with wordpress plugin. Form action is set to a sub-page. I want to check if given email adress is correct - if not, I want to print an alert message.

let emailField = document.querySelector('.email').value;
const regEx = /\S+@\S+\.\S+/;
let submitBtn = document.querySelector('.btn-submit');
let form = document.querySelectorAll('.newsletter-container > form');

function validateEmail() {
  if (regEx.test(emailField) == false) {
    alert('!!!');
    event.preventDefault();
   } else {
    form.submit();
   }
}

submitBtn.addEventListener('click', function (event) {
   validateEmail();
});

My problem is, when I type a correct email adress I still get alert and button default event is prevented from action.

What am I doing wrong?


Viewing all articles
Browse latest Browse all 29758

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>