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

Participant ,
Oct 19, 2017

Copy link to clipboard

Copied

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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);

}

})();

TOPICS
Acrobat SDK and JavaScript, Windows

Views

134

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

Participant ,
Oct 19, 2017

Copy link to clipboard

Copied

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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);

}

})();

TOPICS
Acrobat SDK and JavaScript, Windows

Views

135

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 19, 2017 0
Most Valuable Participant ,
Oct 19, 2017

Copy link to clipboard

Copied

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);

}

})();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2017 0
Participant ,
Oct 20, 2017

Copy link to clipboard

Copied

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

Thank you sir!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 20, 2017 0
try67 LATEST
Most Valuable Participant ,
Oct 20, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 20, 2017 0