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
<input id="email" type="text">
<input id="submit" type="submit" value="Validate">
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 < 1 || emailDomain.length < 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 < emailName.length; i += 1) {
if(validChars.indexOf(emailName.charAt(i)) < 0 ) {
alert("Invalid character in name section");
return false;
}
}
// emailDomain must only include valid chars
for(var j = 0; j < emailDomain.length; j += 1) {
if(validChars.indexOf(emailDomain.charAt(j)) < 0) {
alert("Invalid character in domain section");
return false;
}
}
// Domain must include but not start with .
if(emailDomain.indexOf('.') <= 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 < 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).
validChars
hardly covers the spec: spaces, quotes, @, #, >, ... all are valid chars, too. Validation for an email address is something done best server-side, because JS is easily bypassed + Server-side languages are better suited for validation \$\endgroup\$<input type='email' name='email'>
(This is not supported in Safari (yet).) Check the full list of input types. \$\endgroup\$