patternjavascriptMinor
Validating email without Regex
Viewed 0 times
withoutemailregexvalidating
Problem
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
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
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
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
Solution
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.
Context
StackExchange Code Review Q#38305, answer score: 5
Revisions (0)
No revisions yet.