user5999763
user5999763

Reputation:

How do i restrict my sign up to people with a specific email domain in PHP

How do i restrict my sign up to people with a specific email domain in PHP

So for example, only people with a '@amn.com' email address can register

Upvotes: 0

Views: 2271

Answers (3)

Curious Mind
Curious Mind

Reputation: 659

On your server, check the mail input field if it contains the string and ends with it, like this:

$email = $_POST['email'];
$domain = explode('@',$email)[1];
$emailErr = '';
$domain = explode('@',$email)[1];
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || $domain != 'amn.com') {
  $emailErr = "Invalid email"; 
} else {
    //valid - use input
}

For js validation, you can use something like this:

function validateEmail(email)
{
    var splitted = email.match("^(.+)@thisdomainonly\.com$");
    if (splitted == null) return false;
    if (splitted[1] != null)
    {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
        return true;
    }
    return false;
}

Upvotes: 1

larsemil
larsemil

Reputation: 876

<?php
$email = $_POST['email'];
$domain = explode('@',$email)[1];

if($domain != 'amn.com')
{
    die('This domain is not allowed to register')
}

Upvotes: 1

Pieter De Clercq
Pieter De Clercq

Reputation: 1971

Use the following regular expression:

<?php
$email = "[email protected]";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    die("Not a valid e-mail address!");
} else {
    if(empty(preg_match("/@amn.com$/", $email))) {
        die("E-mail must end with @amn.com!");
    } else {
        //valid//
    }
}
?>

Upvotes: 1

Related Questions