NameSilo

Modifying website registration.

Spaceship Spaceship
Watch

ZeniusIVanisher

Established Member
Impact
5
First off, I'm a complete n00b with this kind of stuff, so bare with me ;)

Okay. Say I have a website script. I want to modify it so people can ONLY register if they have 1 specific e-mail domain. for example: it will only allow members with yahoo.com e-mails to register. if you have gmail, it won't work.

is this possible using php? (well, i'd imagine it would be php)

thanks so much in advance
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
Here you go:

PHP:
<?php

/**************************************************************************
* validate an email address 
*
* 1.   email address must have a valid format
* 2.   email address must be from a specific domain
*
* Parameters:
*		$email		The FULL email address, e.g. [email protected]
*		$domain		The valid domain, e.g domain.com, yahoo.com, etc.
*
* Returns:
*		true or false as the case may be.
*
* The test is case INsensitive.
**************************************************************************/
function validate_email($email,$domain)
{
	return !preg_match('/^([0-9a-z]+[-._+&])*[0-9a-z]+(@' . $domain . ')$/i', $email) ? false : true;
}


/**************************************************************************
* TEST CODE BELOW
**************************************************************************/

// define the valid email domain here
define('EMAIL_DOMAIN', 'yahoo.com');

// test address 1
$addr = '[email protected]';

$valid = validate_email($addr,EMAIL_DOMAIN);
print_result($valid);

// test address 2
$addr = '[email protected]';

$valid = validate_email($addr,EMAIL_DOMAIN);
print_result($valid);

function print_result($valid)
{
	echo $valid ? ('Thank you.  Valid '. EMAIL_DOMAIN . " email address entered.\n")
				: ('Please enter a valid '. EMAIL_DOMAIN . " email address\n");
}
?>

The function is only one line. The test code and comments make up most of the post.

The validator is case insensitive, so YAHOO and yahoo and YaHOo .cOm or .COM (etc.) are all valid.

Tested and works :)
 
0
•••
Thanks a lot for this wonderful scripts. Actually I was also looking for this script.
 
0
•••
My pleasure, I had a validator sitting around. Enhancing it was fun. Here's one that has a couple of more features yet (I've been playing :) ). Examples in the function header, description below.

NOTE: bbcode is inserting some odd spaces in the comment header and examples. The actual function code is fine.

1. The $domain parameter can be an array or a string, so you can provide multiple domains to test for, not just one. Pass a string, only one is tested. Pass an array, the whole list is tested.

2. There is a third parameter, indicating whether the list is an ALLOW list, or a DISALLOW list. If the parameter is TRUE, then the email address MUST be from $domain. If the parameter is FALSE, then the email address MUST NOT be from the domains in the list.

It sounds more complicated than it is. The one-line examples are self-explanatory.

Tested and works.

PHP:
<?php

/**************************************************************************
*
* validate_email($email,$domains=null,$allowed=false);
*
* Validate an email address, with an optional allow/disallow domain spec.
*
* Parameters:
*		$email		The FULL email address, e.g. [email protected]
*		$domains	A single domain or an array of domain names
*		$allowed	If true, $domains contains an array of domain names
*					or a single domain name from which $email MUST come.  
*					If false, $domains contains an array of domains
*					or a single domain from which $email MAY not come.
*
* Returns:
*		true or false as the case may be.
*
* The tests are all case INsensitive.
*
* Examples:
*
*		validate_email($email);											any valid email address is allowed
*		validate_email($email,'yahoo.com',true);						ONLY email from YAHOO.COM addresses allowed
*		validate_email($email,'yahoo.com',false);						yahoo.com email addresses are NOT allowed
*		validate_email($email,array('yahoo.com','gmail.com'),true);		yahoo and gmail ONLY allowed
*		validate_email($email,array('yahoo.com','gmail.com'),false);	yahoo and gmail are DISALLOWED
**************************************************************************/
function validate_email($email,$domains=null,$allowed=false)
{
	// basic check first for acceptable format
	if (!preg_match('/^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$/i',$email)) return false;

	// domains to process?
	if ($domains)
	{
		// a list, so flatten it
		if (is_array($domains))
		{
			// empty array? bad input, so fail!
			if (count($domains) == 0) return false;

			// now we have a regex pattern like domain1|domain2|domain3
			$domains = implode('|',$domains);
		}

		// now check, regardless of whether original input was a list or a string
		return !preg_match('/@(' . $domains . ')$/i',$email) ? !$allowed : $allowed;
	}
	else 
		return true;
}
?>
 
0
•••
Impressive stuff. I love the way you've used $allowed in the ternary operator.
 
0
•••
Thanks :)

New code below to correct a minor bug: emails ending with things like .co.uk or .co.in failed to validate. That's fixed, as well as a little further streamlining of the code to do only one preg match. NOTE that the last parameter now defaults to TRUE instead of FALSE.

PHP:
<?
/**************************************************************************
*
* validate_email($email,$domains=null,$allowed=true)
*
* Validate an email address, with an optional allow/disallow domain spec.
*
* Parameters:
*		$email		The FULL email address, e.g. [email protected]
*		$domains	A single domain or an array of domain names
*		$allowed	If true, $domains contains an array of domain names
*					or a single domain name from which $email MUST come.  
*					If false, $domains contains an array of domains
*					or a single domain from which $email MAY not come.
*
* Returns:
*		true or false as the case may be.
*
* The tests are all case INsensitive.
*
* Examples:
*
*		validate_email($email);											any valid email address is allowed
*		validate_email($email,'yahoo.com',true);						ONLY email from YAHOO.COM addresses allowed
*		validate_email($email,'yahoo.com',false);						yahoo.com email addresses are NOT allowed
*		validate_email($email,array('yahoo.com','gmail.com'),true);		yahoo and gmail ONLY allowed
*		validate_email($email,array('yahoo.com','gmail.com'),false);	yahoo and gmail are DISALLOWED
**************************************************************************/
function validate_email($email,$domains=null,$allowed=true)
{
	if (is_array($domains))  // a list, so flatten it
	{
		// empty array? default to all domains
		if (count($domains) == 0)
			$domains = '[-0-9a-z]+[.]+([-0-9a-z]{2,6}+[.]){0,1}[a-z]{2,6}';
		else // now we have a regex pattern like domain1|domain2|domain3
			$domains = implode('|',$domains);
	}
	else if (!$domains)
		$domains = '[-0-9a-z]+[.]+([-0-9a-z]{2,6}+[.]){0,1}[a-z]{2,6}';

	// now check, regardless of whether original input was a list or a string or nothing
	return !preg_match('/^([0-9a-z]+[-._+&])*[0-9a-z]+@(' . $domains . ')$/i',$email) ? !$allowed : $allowed;
}
?>
 
1
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back