Domain specific email validation

regular expression for email domain name validation
email validation regex
email validation in python using regular expression
simple email regex javascript
email validation in javascript
jquery validate email specific domain
email validation logic
regex exclude email domain

I am trying to code a registration form where I am validating the email used to sign up.

In a nutshell, I want to ensure that the email ID used to register, is a company domain, and not something like gmail or yahoo.

I have the following code to check IF an email is a part of the given domain, how can I modify this to check that it ISNT in a given list of domains? (eg : gmail.com, yahoo.com,hotmail.com, etc).

return (bool) preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $domain);

Im thinking it should be along these lines, but not entirely sure :

function validate($email)
{
$error = 0;
$domains = array('gmail.com','yahoo.com','hotmail.com');
foreach($domains as $key=>$value)
{
if(preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $value)
{
$error=1;
}
}
if($error==0)
return true;
else
return false;

EDIT : I tried all the answers given here, the form still submits without a problem no matter what domain I use! (Even a non email seems to work!)

This is how I'm calling the function -

if(isset($_POST['clients_register']))
{

        //Must contain only letters and numbers
        if(!preg_match('/^[a-zA-Z0-9]$/', $_POST['name']))
        {
            $error[]='The username does not match the requirements';        
        }
        //Password validation: must contain at least 1 letter and number. Allows characters !@#$% and be 8-15 characters
        if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,15}$/', $_POST['password']))
        {
            $error[]='The password does not match the requirements';
        }
        //Email validation
        if (validateEmail($_POST['email'])==false)
        {
            $error[]='Invalid E-mail';
        }       
        //Output error in array as each line
        if ( count($error) > 0) 
          {
            foreach ($error as $output) {
               echo "{$output} <br>";
            }
        } else {
            //Syntax for SQL Insert into table and Redirect user to confirmation page
        }
}

Problem is, no matter what I do, the user gets redirected to the confirmation page (Even with a name made of numbers and an email like "table".

You should do that in a separate step. First check if the e-mailaddress has a valid syntax. Than extract the domain and see if it's not in your blacklist.

function validate($email)
{
  if (!preg_match('/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]{2,})(\.[a-z]{2,4})$/i', $email)) return false;

  $domains = array('gmail.com','yahoo.com','hotmail.com');
  list(, $email_domain) = explode('@', $email, 2);
  return !in_array($email_domain, $domains);
}

[Solved] regular expression for specific email domain, Regular Expression to Email Regex matching specific domains. Test String. email@domain.com. Substitution. Expression Flags. ignore case (i) global (g) To prevent problems with receiving the domain validation email from ACM, ensure that your contact information is visible in WHOIS. If your WHOIS listing shows a privacy email address, ensure that email sent to that address is forwarded to your real email address.

PHP

// Suposing that $email is a valid email
function validate($email) {
    $invalidDomains = array('gmail.com','yahoo.com','hotmail.com');
    $parts = explode('@',$email);
    $domain = $parts[1];
    if(!in_array($domain,$invalidDomains)) return true;
    return false;
}

Let me know if it's useful.

Email Regex matching domains, Check email address syntax; Verify that individual domain specific mailbox rules are met; Check for improbable names (vulgar, famous, bogus, or suspicious  When a user is editing a model, that is, an instance of your domain-specific language, the following actions can run validation: Right-click the diagram and select Validate All. Right-click the top node in the Explorer of your DSL and select Validate All. Save the model. Open the model.

function validateEmail($email)
{
    // Etc, just an array of the blacklisted domains
    $blacklistDomains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'googlemail.com']; 
    // Check if the email is valid
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        return false;
    }

    // Split the email after the '@' to get the domain
    $emailParts = explode('@', $email);

    if (in_array(end($emailParts), $blacklistDomains)) {
        return false;
    }

    return true;
}

You'll need a pretty big list of domains.

Email Validation: How it Works, The most common top-level domains were 2 letters long for country-specific domains A lot of regexes for validating email addresses you'll find in various regex  Top-level domain Top-level domains are the highest level of the domain name system for the Internet and is placed after the domain name in an email address. Common top-level domains are: .com .net .org Examples Examples of valid email addresses include: johndoe@domainsample.com

How to Find or Validate an Email Address, It also confirms if a particular email address exists with a reliable domain such as Gmail or Yahoo. This not only helps in organizing and  To verify that the email address is valid, the IsValidEmail method calls the Regex.Replace(String, String, MatchEvaluator) method with the (@)(.+)$ regular expression pattern to separate the domain name from the email address. The third parameter is a MatchEvaluator delegate that represents the method that processes and replaces the matched text.

Email Validation, You can integrate email ID validation into your web forms to correct bad and local-part grammar specific to the domain name of the email service provider. Email Checker is a simple tool for verifying an email address. It's free and quite easy to use. Just enter the email address and hit check button. Then it tells you whether the email address is real or not.

Email ID Validation, Is there any way i can add some kind of javascript validation on the register page email textbox so that it prevents users to type specific email  The service validates email addresses based on advanced features like mailbox detection, syntax checks, spell checks, and local-part grammar specific to the domain name of the email service provider. All of this happens thanks to Mailgun’s real-time API for developers.

Comments
  • $email_parts = split('@',$email); if (in_array($email_parts[1],$domains) ............
  • Add "googlemail.com", its another domain of gmail...
  • What's wrong with these big mail services? Using a domain not on your list does not guarantee that it belongs to a company. And companies might use Google mail under their domain, too. I wonder what effect you want to get from blacklisting these.
  • It is specifically to isolate small time businesses that do not have their own domain. Its complex to explain, but I appreciate the help regardless!
  • I tried this, i've edited my original post with the way Im calling the email, still doesn't seem to work!
  • The validation function is correct. Is the validation done at all? The problem lies elsewhere. What if I enter a faulty username (like 'W@rnold')? What is $_POST['clients_register']? Why not do if($_SERVER['REQUEST_METHOD'] === 'POST')) { instead?
  • It was the default when I used dreamweaver's insert record option! But i'll try it your way and let you know how it works!