A simple e-mail address validation function, testing for character positions, without using regular expressions.
The function tests for the presence of the characters @ and the full stop, returning either true or false.
For the full stop check it looks for its presence after the 3rd character in the string.
The @ symbol, is tested to ensure it exists.
The position test of both of these characters could be readily changed.
If both of these characters exist then the function returns true, otherwise false.
function isValidEmail(str) {
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
str is the string passed to the function isValidEmail for testing.
The single line has the two tests, combining their results with && and returning the result.
On the left str.indexOf(“.”) > 2) tests for the full stop and on the right (str.indexOf(“@”) > 0) checks to ensure that the @ symbol is present.
For the function to return true both parts of the test must be valid.


