Skip to content

Instantly share code, notes, and snippets.

@johnmorris
Created April 28, 2015 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnmorris/4bbe9800c0743b93fa7a to your computer and use it in GitHub Desktop.
Save johnmorris/4bbe9800c0743b93fa7a to your computer and use it in GitHub Desktop.
How validate an email address in PHP (by domain)
<?php
/**
* Validates email addresses
*
* This class is used to validate email addresses using several different criteria.
*
* @version 1.0
* @author John Morris <support@johnmorrisonline.com>
*/
class validate_email {
/**
* Accepted domains
* @access private
* @var array
*/
private $accepted_domains;
/**
* The class constructor
*
* This sets up the class
*/
public function __construct() {
// Set the accepted domains property
$this->accepted_domains = array(
'unl.edu', 'uni.edu'
);
}
/**
* Validate email address by domain
*
* Checks if the email address belongs to an accepted domain
*/
public function validate_by_domain($email_address) {
// Get the domain from the email address
$domain = $this->get_domain( trim( $email_address ) );
// Check if domain is accepted. Return return if so
if ( in_array( $domain, $this->accepted_domains ) ) {
return true;
}
return false;
}
/**
* Get the domain
*
* Gets the domain from an email address
*/
private function get_domain($email_address) {
// Check if a valid email address was submitted
if ( ! $this->is_email( $email_address ) ) {
return false;
}
// Split the email address at the @ symbol
$email_parts = explode( '@', $email_address );
// Pop off everything after the @ symbol
$domain = array_pop( $email_parts );
return $domain;
}
/**
* Check email address
*
* Checks if the submitted value is a valid email address
*/
private function is_email($email_address) {
// Filter submitted value to see if it's a proper email address
if ( filter_var ( $email_address, FILTER_VALIDATE_EMAIL ) ) {
return true;
}
return false;
}
}
<?php
require_once( dirname( __FILE__ ) . '/class-validate-email.php' );
$validate_email = new validate_email;
// Valid
if ( $validate_email->validate_by_domain('justin@unl.edu ') ) {
echo '<p>This is valid!</p>';
} else {
echo '<p>This is NOT valid!</p>';
}
// Valid
if ( $validate_email->validate_by_domain('jason@uni.edu') ) {
echo '<p>This is valid!</p>';
} else {
echo '<p>This is NOT valid!</p>';
}
// Not valid
if ( $validate_email->validate_by_domain('ramone@ui.edu') ) {
echo '<p>This is valid!</p>';
} else {
echo '<p>This is NOT valid!</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment