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
Asked
Active
Viewed 447 times
-1
-
you're probs not the first one with this issue – Taylor A. Leach Jul 16 '18 at 21:16
-
regex: https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript – Ryan Schlueter Jul 16 '18 at 21:33
-
Possible duplicate of [How to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – Tim Wißmann Jul 16 '18 at 22:36
1 Answers
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