| Viewed 66 times

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
Mohammad Qandeel
Answered
17

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

 


Related Questions