0

I have client side email validation script Javascript+RegEx, it works fine, but I want to exclude certain domains while validating, namely all Apple domains since they do not work (emails sent to these addresses are deleted without any notice): @apple.com, @me.com, @icloud.com, @mac.com.

I found appropriate questions here, but still they are not the same I am asking for help. Please, help to implement this

Can it be done via RegEx modification, or I have to use loop and search substrings (@apple.com, @me.com, @icloud.com, @mac.com) after the main email validation is done?

function verifyMe(){
var msg='';

var email=document.getElementById('email').value;
if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) || 
document.getElementById('email').value=='')
{
msg+='- Invalid Email Address: '+email+'\n\n';
document.getElementById('Eemail').style.color='#ffffff';
}
else
document.getElementById('Eemail').style.color='#bbb'
 
if(msg!='')
return false; 
else
{
search_code(); //it's ok go ahead
return true;
}
}
IVGSoft S
  • 35
  • 2
  • 9
  • Could you share you attempts and tell us why they didn't work for you? – PeeHaa Oct 15 '14 at 17:39
  • I am not sure which approach to use - RegEx mod or loop to search substring? since I want to exclude an array of domains, not a single domain. I am lost with RegEx – IVGSoft S Oct 15 '14 at 17:42
  • Maybe parse using [a proper email address parser](https://www.npmjs.org/package/email-addresses) instead of trying to use regular expressions. Getting a regex right is going to be hard. For example, `FOO@APPLE.COM` is equivalent to `FOO@apple.com` since hostnames are case-insensitive, and `FOO@apple.com.` is also valid but just says that `apple.com` should not be resolved using a suffix search-list. – Mike Samuel Oct 15 '14 at 18:23
  • Those two subtleties are just things I noticed before getting into what can appear before the `@`, and whether you're breaking on the right `@`. – Mike Samuel Oct 15 '14 at 18:29
  • yes, case-sensitivity is an issue in case of RegEx, so better to use array check, by lowercasing input – IVGSoft S Oct 15 '14 at 18:41

3 Answers3

3

Both approaches would work.

For the regex one, just insert the following part after the @ in the regex (negative lookahead):

(?!(?:apple|me|icloud|mac)\.com$)

But a better regex overall would be:

^\w+[-\.\w]*@(?!(?:apple|me|icloud|mac)\.com$)\w+[-\.\w]*?\.\w{2,4}$

For the other approach, the following should work:

function isValidMailAddress(email) {
    var match = /^\w+[-\.\w]*@(\w+[-\.\w]*?\.\w{2,4})$/.exec(email);
    if (!match)
        return false;

    var forbiddenDomains = ["apple.com", "me.com", "icloud.com", "mac.com"];
    if (forbiddenDomains.indexOf(match[1].toLowerCase()) >= 0)
        return false;

    return true;
}

It's up to you to decide which approach you feel most comfortable with.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
0

You can use jQuery.inArray() for checking email with a specific domain name.

var email ="abc@xyz.edu.au" 
var str = email.split('@').slice(1);
var allowedDomains = ['xyz.edu.au','abc.edu.au'];

if($.inArray(str[0], allowedDomains) === -1) {
   alert('email is allowed.');
}
else{
   alert('email not allowed.');
}
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Praveen Barawal
  • 191
  • 1
  • 1
  • 5
0

I updated @Lucas answer to match any type of country domain (apple.com, apple.de etc.).

Moreover it should be more robust because its closer to W3C standard: https://emailregex.com/

^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@(?!(?:yahoo|gmail|icloud|web|googlemail|aol|zoho|protonmail|outlook|hotmail|gmx|mail)[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]{1,10}$)[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$
ohlr
  • 1,839
  • 1
  • 13
  • 29