Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Oct 19, 2017 Oct 19, 2017

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.

TOPICS
Acrobat SDK and JavaScript , Windows
644
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 19, 2017 Oct 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);

}

})();

Translate
Community Expert ,
Oct 19, 2017 Oct 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);

}

})();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 20, 2017 Oct 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2017 Oct 20, 2017
LATEST

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

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