Re: A cleaner way to write this email validation code?

Gerard_Donnell7
Level 1

A cleaner way to write this email validation code?

Hi Guys, 

Can anyone help me slim down this code I came across? 

It checks for both business emails ( I know its not all free emails, just the usual ones its checking) and a valid email address. 

The issue I see is that you need to insert all the email possibilities i.e. "yahoo.com",  "yahoo.co.uk". I think it would be better to make the validation check for contains "yahoo". 

Love to know your thoughts or advice?

<script>

MktoForms2.whenReady(function(form) {
form.onValidate(function(builtInValid) {
if (!builtInValid) return;
form.submittable(true);

var vals = form.vals();
var invalidDomains = ["gmail.com", "gmail.co.uk",
"gmail.om", "yahoo.co", "yahoo.com", "yahoo.co.uk",
"hotmail.cm", "hotmail.co", "hotmail.com", "hotmail.co.uk",
"outlook.cm", "outlook.co", "outlook.com", "outlook.co.uk",
"aol.cm", "aol.co", "aol.om", "aol.com", "live.com", "live.co"
],

emailExtendedValidationError =
'Please enter a Business email address.',

emailExtendedValidationError1 =
'Email seems to be incorrect.',

emailField = 'Email',
emailFieldSelector = '#' + emailField;
var invalidDomainRE = new RegExp('@(' + invalidDomains.join(
'|') + ')$', 'i');
if (invalidDomainRE.test(form.vals()[emailField])) {
form.showErrorMessage(emailExtendedValidationError,
form.getFormElem().find(emailFieldSelector)
);
form.submittable(false);
}

var emailRegExp =
/^[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,}$/;

var validEmail = emailRegExp.test(vals.Email);
if (!validEmail) {
form.showErrorMessage(emailExtendedValidationError1,
form.getFormElem().find(emailFieldSelector)
);
form.submittable(false);
}
else {
form.submittable(form.submittable() !== false);
}
});
form.onSubmit(function(form) {
});
});

</script>

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

Re: A cleaner way to write this email validation code?

Please highlight your code using the Syntax Highlighter, can't read it otherwise.

Tony_Mayse
Level 3

Re: A cleaner way to write this email validation code?

You can slim the list down by checking for the first part of the domain. This line builds a regex

var invalidDomainRE = new RegExp('@(' + invalidDomains.join(
'|') + ')$', 'i');

which explicitly looks for the end of the string by using the $. Change ')$' to ').*' and then you abbreviate your list of domains  like this:

var invalidDomains = ["gmail.c", "yahoo.c", "hotmail.c", 
"outlook.c", "aol.c", "live.c"]‍

I haven't tested that code so there could be typos, but that technique will work.

SanfordWhiteman
Level 10 - Community Moderator

Re: A cleaner way to write this email validation code?

But even if the literal dots in invalidDomains were properly escaped (the original regexp creation is broken because it doesn't slash-escape the dots) your update would match outlook.compass.org and live.catering. Only known Microsoft-/Gmail-/Yahoo-owned freemail domains should be matched, even if you aren't using the whole freemail list.

Anyway, the original code is still unreadable to me.

Tony_Mayse
Level 3

Re: A cleaner way to write this email validation code?

Sanford Whiteman‌, 100% valid!

Gerard Donnelly‌, it might make sense to actually expand the code. I did a little searching and found this list which seems like it might serve you well. I'll leave it up to you if you really want to exclude all these domains. Another technique might be a pop-up that asks the form-filler to identify what kind of email it is: commercial, professional, or personal for example. Then if they select personal, ask for a work email.

That said, there's another technique you might find helpful: minification.

With minification, this:

MktoForms2.whenReady(function(form) {
form.onValidate(function(builtInValid) {
if (!builtInValid) return;
form.submittable(true);

var vals = form.vals();
var invalidDomains = [
/* Default domains included */
"aol.com", "att.net", "comcast.net", "facebook.com", "gmail.com", "gmx.com", "googlemail.com",
"google.com", "hotmail.com", "hotmail.co.uk", "mac.com", "me.com", "mail.com", "msn.com",
"live.com", "sbcglobal.net", "verizon.net", "yahoo.com", "yahoo.co.uk",

/* Other global domains */
"email.com", "fastmail.fm", "games.com" /* AOL */, "gmx.net", "hush.com", "hushmail.com", "icloud.com",
"iname.com", "inbox.com", "lavabit.com", "love.com" /* AOL */, "outlook.com", "pobox.com", "protonmail.ch", "protonmail.com", "tutanota.de", "tutanota.com", "tutamail.com", "tuta.io",
"keemail.me", "rocketmail.com" /* Yahoo */, "safe-mail.net", "wow.com" /* AOL */, "ygm.com" /* AOL */,
"ymail.com" /* Yahoo */, "zoho.com", "yandex.com",

/* United States ISP domains */
"bellsouth.net", "charter.net", "cox.net", "earthlink.net", "juno.com",

/* British ISP domains */
"btinternet.com", "virginmedia.com", "blueyonder.co.uk", "freeserve.co.uk", "live.co.uk",
"ntlworld.com", "o2.co.uk", "orange.net", "sky.com", "talktalk.co.uk", "tiscali.co.uk",
"virgin.net", "wanadoo.co.uk", "bt.com",

/* Domains used in Asia */
"sina.com", "sina.cn", "qq.com", "naver.com", "hanmail.net", "daum.net", "nate.com", "yahoo.co.jp", "yahoo.co.kr", "yahoo.co.id", "yahoo.co.in", "yahoo.com.sg", "yahoo.com.ph", "163.com", "yeah.net", "126.com", "21cn.com", "aliyun.com", "foxmail.com",

/* French ISP domains */
"hotmail.fr", "live.fr", "laposte.net", "yahoo.fr", "wanadoo.fr", "orange.fr", "gmx.fr", "sfr.fr", "neuf.fr", "free.fr",

/* German ISP domains */
"gmx.de", "hotmail.de", "live.de", "online.de", "t-online.de" /* T-Mobile */, "web.de", "yahoo.de",

/* Italian ISP domains */
"libero.it", "virgilio.it", "hotmail.it", "aol.it", "tiscali.it", "alice.it", "live.it", "yahoo.it", "email.it", "tin.it", "poste.it", "teletu.it",

/* Russian ISP domains */
"mail.ru", "rambler.ru", "yandex.ru", "ya.ru", "list.ru",

/* Belgian ISP domains */
"hotmail.be", "live.be", "skynet.be", "voo.be", "tvcablenet.be", "telenet.be",

/* Argentinian ISP domains */
"hotmail.com.ar", "live.com.ar", "yahoo.com.ar", "fibertel.com.ar", "speedy.com.ar", "arnet.com.ar",

/* Domains used in Mexico */
"yahoo.com.mx", "live.com.mx", "hotmail.es", "hotmail.com.mx", "prodigy.net.mx",

/* Domains used in Canada */
"yahoo.ca", "hotmail.ca", "bell.net", "shaw.ca", "sympatico.ca", "rogers.com",

/* Domains used in Brazil */
"yahoo.com.br", "hotmail.com.br", "outlook.com.br", "uol.com.br", "bol.com.br", "terra.com.br", "ig.com.br", "itelefonica.com.br", "r7.com", "zipmail.com.br", "globo.com", "globomail.com", "oi.com.br"
]

emailExtendedValidationError =
'Please enter a Business email address.',

emailExtendedValidationError1 =
'Email seems to be incorrect.',

emailField = 'Email',
emailFieldSelector = '#' + emailField;
var invalidDomainRE = new RegExp('@(' + invalidDomains.join(
'|') + ')$', 'i');
if (invalidDomainRE.test(form.vals()[emailField])) {
form.showErrorMessage(emailExtendedValidationError,
form.getFormElem().find(emailFieldSelector)
);
form.submittable(false);
}

var emailRegExp =
/^[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,}$/;

var validEmail = emailRegExp.test(vals.Email);
if (!validEmail) {
form.showErrorMessage(emailExtendedValidationError1,
form.getFormElem().find(emailFieldSelector)
);
form.submittable(false);
}
else {
form.submittable(form.submittable() !== false);
}
});
form.onSubmit(function(form) {
});
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

becomes this:

MktoForms2.whenReady(function(o){o.onValidate(function(e){if(e){o.submittable(!0);var m=o.vals();emailExtendedValidationError="Please enter a Business email address.",emailExtendedValidationError1="Email seems to be incorrect.",emailField="Email",emailFieldSelector="#"+emailField,new RegExp("@("+["aol.com","att.net","comcast.net","facebook.com","gmail.com","gmx.com","googlemail.com","google.com","hotmail.com","hotmail.co.uk","mac.com","me.com","mail.com","msn.com","live.com","sbcglobal.net","verizon.net","yahoo.com","yahoo.co.uk","email.com","fastmail.fm","games.com","gmx.net","hush.com","hushmail.com","icloud.com","iname.com","inbox.com","lavabit.com","love.com","outlook.com","pobox.com","protonmail.ch","protonmail.com","tutanota.de","tutanota.com","tutamail.com","tuta.io","keemail.me","rocketmail.com","safe-mail.net","wow.com","ygm.com","ymail.com","zoho.com","yandex.com","bellsouth.net","charter.net","cox.net","earthlink.net","juno.com","btinternet.com","virginmedia.com","blueyonder.co.uk","freeserve.co.uk","live.co.uk","ntlworld.com","o2.co.uk","orange.net","sky.com","talktalk.co.uk","tiscali.co.uk","virgin.net","wanadoo.co.uk","bt.com","sina.com","sina.cn","qq.com","naver.com","hanmail.net","daum.net","nate.com","yahoo.co.jp","yahoo.co.kr","yahoo.co.id","yahoo.co.in","yahoo.com.sg","yahoo.com.ph","163.com","yeah.net","126.com","21cn.com","aliyun.com","foxmail.com","hotmail.fr","live.fr","laposte.net","yahoo.fr","wanadoo.fr","orange.fr","gmx.fr","sfr.fr","neuf.fr","free.fr","gmx.de","hotmail.de","live.de","online.de","t-online.de","web.de","yahoo.de","libero.it","virgilio.it","hotmail.it","aol.it","tiscali.it","alice.it","live.it","yahoo.it","email.it","tin.it","poste.it","teletu.it","mail.ru","rambler.ru","yandex.ru","ya.ru","list.ru","hotmail.be","live.be","skynet.be","voo.be","tvcablenet.be","telenet.be","hotmail.com.ar","live.com.ar","yahoo.com.ar","fibertel.com.ar","speedy.com.ar","arnet.com.ar","yahoo.com.mx","live.com.mx","hotmail.es","hotmail.com.mx","prodigy.net.mx","yahoo.ca","hotmail.ca","bell.net","shaw.ca","sympatico.ca","rogers.com","yahoo.com.br","hotmail.com.br","outlook.com.br","uol.com.br","bol.com.br","terra.com.br","ig.com.br","itelefonica.com.br","r7.com","zipmail.com.br","globo.com","globomail.com","oi.com.br"].join("|")+")$","i").test(o.vals()[emailField])&&(o.showErrorMessage(emailExtendedValidationError,o.getFormElem().find(emailFieldSelector)),o.submittable(!1));/^[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,}$/.test(m.Email)?o.submittable(!1!==o.submittable()):(o.showErrorMessage(emailExtendedValidationError1,o.getFormElem().find(emailFieldSelector)),o.submittable(!1))}}),o.onSubmit(function(o){})});

You could also minify your original script, of course.