Validating email without Regex I'm trying to improve my JavaScript (I'm usually a copy/paste guy but can do basic DOM stuff with jQuery too), so I decided to try and make a function to validate an email address without using Regex.

The code seems to work, but I'm interested in hearing how it could be improved.

I'll include the code below, but you can run/fork it on Codepen.

HTML

&lt;input id="email" type="text"&gt;
&lt;input id="submit" type="submit" value="Validate"&gt;


JS

// Function: valid email address without regex
function isvalidemail(email) {

    // Get email parts
    var emailParts = email.split('@');

    // There must be exactly 2 parts
    if(emailParts.length !== 2) {
        alert("Wrong number of @ signs");
        return false;   
    }

    // Name the parts
    var emailName = emailParts[0];
    var emailDomain = emailParts[1];

    // === Validate the parts === \\

    // Must be at least one char before @ and 3 chars after
    if(emailName.length &lt; 1 || emailDomain.length &lt; 3) {
        alert("Wrong number of characters before or after @ sign");
        return false;
    }

    // Define valid chars
    var validChars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','.','0','1','2','3','4','5','6','7','8','9','_','-'];

    // emailName must only include valid chars
    for(var i = 0; i &lt; emailName.length; i += 1) {
        if(validChars.indexOf(emailName.charAt(i)) &lt; 0 ) {
            alert("Invalid character in name section");
            return false;   
        }
    }
    // emailDomain must only include valid chars
    for(var j = 0; j &lt; emailDomain.length; j += 1) {
        if(validChars.indexOf(emailDomain.charAt(j)) &lt; 0) {
            alert("Invalid character in domain section");
            return false;   
        }
    }

    // Domain must include but not start with .
    if(emailDomain.indexOf('.') &lt;= 0) {
        alert("Domain must include but not start with .");
        return false;
    }

    // Domain's last . should be 2 chars or more from the end
    var emailDomainParts = emailDomain.split('.');
    if(emailDomainParts[emailDomainParts.length - 1].length &lt; 2) {
        alert("Domain's last . should be 2 chars or more from the end");
        return false;   
    }

    alert("Email address seems valid");
    return true;
}

document.getElementById('submit').onclick = function() {
    var email = document.getElementById('email').value;
    isvalidemail(email);
};


One thing I know could be improved is where I repeat basically the same code twice to validate the characters in each section. I am more interested in hearing if the way I've structured the function and sections could be done better, as the exercise for me was more about writing well-structured JavaScript and getting familiar with some of the built-in functions than about writing a perfect validator (which is also the reason I didn't use Regex).
Read the (email address: syntax) article on Wikipedia  for what is allowed. Your current validation will produce a lot of fails for valid email addresses.