-1

I need to validate the position of the @ symbol when the form is submitted. If Email address is not in the following format ‘@’ position<1 OR ‘ .’position< ’@’ position+2 OR ‘.’position+2>= Email length an alert should pop up

she_grab
  • 77
  • 9

1 Answers1

0

You need something similar to this to validate the email manually using given conditions.

function validateEmail() {
  var emailInput = document.getElementById('email');
  var atPos = emailInput.value.indexOf("@");
  var dotPos = emailInput.value.indexOf(".");
  var emailLength = emailInput.value.length;
  if (atPos < 1 || dotPos < atPos + 2 || dotPos + 2 > emailLength) {
    alert("Invalid Email format");
    return false;
  }
  return true;
}

BTW always recommended to use regex method since it's the proper way to do these type of validations.

Nuwan Alawatta
  • 1,812
  • 21
  • 29