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.
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);
}
})();
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);
}
})();
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!
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