I have the following javascript function validateEmail(value). The goal of the function is to validate the users inputted email as they are typing. So if someone puts nfg@*gmail.com the email will be displayed with the * char crossed out. Anytime there is an invalid character the display will show it striked out. This must be updated on every key push. Here's a JSFiddle: https://jsfiddle.net/d4kgejym/1/
This is one of my first code reviews so please comment below if anymore information is needed.
Assumptions about email addresses: (not true in real world)
A valid email is defined as follows:
^[a-z0-9]*[@][a-z]*[.][a-z]{1,3}$
function validateEmail() {
var value = $('#emailInput').val();
// TODO unsupported case: nfgallim$@ :(
// case 5 where has @ and . with chars after .
// [email protected]
if (value.includes('@') &&
value.substr(value.indexOf('@')).length > 1 &&
value.includes('.') &&
value.substr(value.indexOf('.')).length > 1) {
$('#emailDisplay').html(validateCase5Regex(value));
}
// case 4 where has @ with chars after and . with no chars after .
// nfgallimore@gmail.
else if (value.includes('@') &&
value.substr(value.indexOf('@')).length > 1 &&
value.includes('.') &&
value[value.length - 1] === '.') {
$('#emailDisplay').html(validateCase4Regex(value));
}
// case 3 where has @ with chars after @
// nfgallimore@gmail
else if (value.includes('@') &&
value.substr(value.indexOf('@')).length > 1) {
$('#emailDisplay').html(validateCase3Regex(value));
}
// case 2 where has only @ with no chars after @
// nfgallimore@
else if (value.includes('@') &&
value[value.length - 1] === '@') {
$('#emailDisplay').html(validateCase2Regex(value));
}
// case 1 with just ^[a-z0-9]*$
// nfgallimore
else {
$('#emailDisplay').html(validateCase1Regex(value));
}
}
// [email protected]
function validateCase5Regex(value) {
var domainTypeRegex = new RegExp('^[a-z]$');
var stringRegex = new RegExp('^[a-z0-9]*[@][a-z]*[.][a-z]{1,3}$');
var periodIndex = value.indexOf('.'); // first period
var firstStr = value.slice(0, periodIndex + 1);
var substr = value.substr(periodIndex + 1);
return !value.match(stringRegex) ?
validateCase4Regex(firstStr) +
GetHtmlErrorWithMaxLength(substr, domainTypeRegex, 3) : value;
}
// nfgallimore@gmail.
function validateCase4Regex(value) {
var stringRegex = new RegExp('^[a-z0-9]*[@][a-z]*[.]$');
// nfgallimore@gmail
var strWithoutPeriodSymbol = value.slice(0, value.length - 1);
return !value.match(stringRegex) ?
validateCase3Regex(strWithoutPeriodSymbol) + '.' : value;
}
// nfgallimore@gmail
function validateCase3Regex(value) {
var domainRegex = new RegExp('^[a-z]*$');
var stringRegex = new RegExp('^[a-z0-9]*[@][a-z]*$');
var atIndex = value.indexOf('@'); // split on first occurrence of @
var firstStr = value.slice(0, atIndex); // nfgallimore@
var domainStr = value.substr(atIndex + 1) // gmail
// apply secondStrRegex to second str
return !value.match(stringRegex) ?
validateCase2Regex(firstStr) + GetHtmlError(domainStr, domainRegex) : value;
}
// nfgallimore@
function validateCase2Regex(value) {
var regex = new RegExp('^[a-z0-9]*[@]$');
var strWithoutAtSymbol = value.slice(0, value.length); // nfgallimore
return !value.match(regex) ?
validateCase1Regex(strWithoutAtSymbol) + '@' : value;
}
// nfgallimore
function validateCase1Regex(value) {
var regex = new RegExp('^[a-z0-9]*$');
return !value.match(regex) ?
GetHtmlError(value, regex) : value;
}
// Helper function that returns html.
// Chars that fail the regex get striken out
function GetHtmlError(value, regex) {
var letters = '';
for (var i = 0, len = value.length; i < len; i++) {
if (value[i].match(regex)) {
letters += value[i];
} else {
letters += '<span class="strike">' + value[i] + '</span>';
}
}
return letters;
}
// Helper function that returns html.
// Chars that fail the regex or exceed length get striken out
function GetHtmlErrorWithMaxLength(value, regex, length) {
var letters = '';
for (var i = 0, len = value.length; i < len; i++) {
if (value[i].match(regex) && i < length) {
letters += value[i];
}
else {
letters += '<span class="strike">' + value[i] + '</span>';
}
}
return letters;
}
<input>elements type toemailand let the browser worry about the validation. developer.mozilla.org/en-US/docs/Web/HTML/Element/… \$\endgroup\$