0

This is the code i have so far:

i have set up the function to detect the input is a valid email input.

i have set up the code to print an error message when there is no input.

No message is needed if email is correct, only when email is invalid.

"use strict";
function validEmail(email) {
    var re =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function validate() {
  var errorMsg = "";
  var emailboxInput = document.getElementById("email");
  var emailbox = emailboxInput.value.trim();
  emailboxInput.value = emailbox;


  if (emailbox === "") {
    errormsg += "Email cannot be empty.<br>";
  }
  if (emailbox === (!validEmail(email))
   {
    }
else {
      errorMessage += "Provide a valid email address<br>";
    }

    else {
      errorMsg += "Provide a valid email address<br>";
    }
  return errorMsg;
}



// submit button
// msg = html area where error messages are displayed
var sendBtn = document.getElementById("form-send");
sendBtn.onclick = function () {
  var msgArea = document.getElementById("msg");
  var msg = validate();
  if (msg === "") {
    return true;
  } else {
    msgArea.innerHTML = msg;
    return false;
  }
};
  • 1
    Please do not use regex to validate emails: https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression – Terry Feb 18 '22 at 23:03
  • `if (emailbox === (!validEmail(email))` should be `if (!validEmail(email))` – kmoser Feb 18 '22 at 23:04

0 Answers0