How to checks if string a valid email address in JavaScript?
The string must contain an @ character. The string must contain a . character. The @ must have at least one character in front of it. The . and the @ must be in the appropriate places.
1 Answer
There are many ways to do this, you can try this function
function validateEmail(str) {
if (str.indexOf('@') < 1
|| str.indexOf('.') < 0
|| str.indexOf('@') > str.lastIndexOf('.')
) return false;
return true;
}
Example
validateEmail('a@gmail.com') // true
answer Link