ello There, i want to ask, how can i customize this function to allow only certain email domain.

like for instance A & B wants to register,
A email = test@ymail.com
B email = test@onlyalloweddomain.com

how can i validate that?

here's my function

function validate_values() {
        // Create the array which contains the Language variable
        $error = array();

        // Define the Language variable for each type of error
        if($this->verify_if_user_exist() !== 0) {
            $error[] .= 'user_exists';
        }
        if($this->verify_if_email_exists() !== 0) {
            $error[] .= 'email_exists';
        }
        if(empty($this->username) && empty($this->password) && empty($email)) {
            $error[] .= 'all_fields';
        }
        if(strlen($this->password) <= 2) {
            $error[] .= 'password_too_short';
        }
        if(!ctype_alnum($this->username)) {
            $error[] .= 'user_alnum';
        }
        if(strlen($this->username) <= 2 || strlen($this->username) >= 33) {
            $error[] .= 'user_too_short';
        }
        if(!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
            $error[] .= 'invalid_email';
        }
        if($this->verify_captcha() == false) {
            $error[] .= 'invalid_captcha';
        }

        return $error;
    } 

THANKS!!

Recommended Answers

All 10 Replies

You can use bunch of php inbuilt functions. Here is an example of preg_match().

if (preg_match('|@onlyalloweddomain.com$|', $registrantMailAddress;
{
    // continue with registration
}

I found it on sof.

Why not try this out?

<?php
    $denied_hostnames = array("gmail.com", "yahoo.com");

    foreach ($denied_hostnames as $hn)
    {
        if (strstr($_POST['email'], "@" . $hn))
        {
            $errors[] = "Sorry bud! We don't accept " . $hn . " email addresses";
        }
    }

You could also do:

<?php
    $allowed_hostnames = array("mydomain.com", "mydomain.net");

    $good_hostname = false;

    foreach ($allowed_hostnames as $hn)
    {
        if (strstr($_POST['email'], $hn))
        {
            // Makin' it true here! :P
            $good_hostname = true;
        }else{
            if ($good_hostname==false)
            {
                // Keepin' it false here!
                $good_hostname = false; // Set it to false anyway
            }
        }
    }

    if (($good_hostname==true)==false)
    {
        $errors[] = "denied_email";
    }

For my last post, you may aswell remove the worthless 'if'.

<?php
    $allowed_hostnames = array("mydomain.com", "mydomain.net");

    $good_hostname = false;
    foreach ($allowed_hostnames as $hn)
    {
        if (strstr($_POST['email'], $hn))
        {
            // Makin' it true here! :P
            $good_hostname = true;
        }else{
            // TIMEEEEEE TO REST!
        }
    }

    if (($good_hostname==true)==false)
    {
        $errors[] = "denied_email";
    }

Hope this helped. If it did, I'd appreciate a rate up.

@matrixdevuk be careful with this solution, because strstr() will check only for the first occurence of the searched pattern, so if I submit something like this:

mydomain.net@evil.org

it will pass this check.

Docs: http://php.net/strstr

It can be solved just putting @ sign infront of domain name:
@unwantedmail.tld

Not even because, according to the RFC3696 an email like this one:

test\@mydomain.com@evil.org

is still a valid email address.

commented: Helped me with better understanding of mailing services +2

I actually meant to put "@" . $hn in my post.
You could easilly check how many @'s there are in the string, or better yet, remove all \'s.

I solved the problem using strpos
Thanks guys!

Hi ignnniter, just a suggestion: strpos can lead to the same problem of the strstr approach, because it will check only for the first occurrence.

Tpojka suggested preg_match which is a better solution because you can use a pattern to return the last at-sign segment in the email i.e. the domain part. Here's an example:

<?php

$whitelist = array(
        '@some.tld',
        '@mine.tld'
    );

function checkMailDomain($mail, $whitelist)
{
    if(preg_match('/@(?!.*@).*+/', $mail, $matches) == 1)
    {
        return in_array($matches[0], $whitelist) ? true:false;
    }

    return false;
}

$mail = 'example\@some.tld@evil.org';
echo checkMailDomain($mail, $whitelist) === true ? $mail .' is allowed': $mail .' is not allowed';

$mail = 'example@mine.tld';
echo checkMailDomain($mail, $whitelist) === true ? $mail .' is allowed': $mail .' is not allowed';

It will output:

example\@some.tld@evil.org is not allowed
example@mine.tld is allowed

The above pattern /@(?!.*@).*+/ works like this: match the at-sign and all the following text that is not followed by another at-sign.

Reference: http://www.rexegg.com/regex-disambiguation.html

Bye!

isset(in_array(end(explode('@', $mail)), array('disallowed.tld', 'notwanted.tld'))) ? 'noPe' : 'yuP';
//or
if (in_array(end(explode('@', $mail)), array('disallowed.tld', 'notwanted.tld')))
{
    echo 'We don\'t want your registration here.';
}
else
{
    // define('ADMIN', $mail);
}
commented: good +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.