Skip to main content
Inspiring
October 19, 2017
Answered

Validating an email address with more than one domain name and eliminating the rest

  • October 19, 2017
  • 1 reply
  • 692 views

I would like to validate an email address using more than one specific domain name.  My current code (which I found here) for validating an email address with one specific domain name is:

(function (){

var sDomain = "mail.mil";

if(!event.value) return;

var sEnteredDomain = event.value.split("@")[1];

if (typeof sEnteredDomain === "undefined" || sEnteredDomain.toLowerCase() !== sDomain){

event.rc = false;

app.alert("Your email address must end with:  " + sDomain, 3);

}

})();

FYI: I am using this code in a PDF form.

This topic has been closed for replies.
Correct answer try67

Try this:

(function (){

var sDomains = ["mail.mil", "mail.gov", "mail.org"];

if(!event.value) return;

var sEnteredDomain = event.value.split("@")[1];

if (typeof sEnteredDomain === "undefined" || sDomains.indexOf(sEnteredDomain.toLowerCase())==-1 ){

event.rc = false;

app.alert("Your email address must end with one of the following domains:  " + sDomains.join("\n"), 3);

}

})();

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 19, 2017

Try this:

(function (){

var sDomains = ["mail.mil", "mail.gov", "mail.org"];

if(!event.value) return;

var sEnteredDomain = event.value.split("@")[1];

if (typeof sEnteredDomain === "undefined" || sDomains.indexOf(sEnteredDomain.toLowerCase())==-1 ){

event.rc = false;

app.alert("Your email address must end with one of the following domains:  " + sDomains.join("\n"), 3);

}

})();

SIPRNet11Author
Inspiring
October 20, 2017

Duh!  So you set it up like an array, is that correct?  What does the -1 do in line 5?

Thank you sir!

try67
Community Expert
Community Expert
October 20, 2017

Yes, I converted the sDomain string to an array, renamed it, and adjusted the rest of the code to work with it.

Regarding the -1, read this: JavaScript Array indexOf() Method