5

I know email validation is one of those things which is not the funniest thing on the block. I'm starting up a website and i want to limit my audience to only the people in my college and i also want a preferred email address for my user. So this is a two part question.

  1. Is there a really solid php function out there for email validation?

  2. Can I validate an email from a specific domain. I dont want to just check if the domain exists, because I know www.mycollege.edu exists already. Is there really anyway to validate that the user has a valid @mycollege.edu web address?

peterh
  • 11,875
  • 18
  • 85
  • 108
Ben
  • 1,233
  • 2
  • 14
  • 17
  • possible duplicate of what's getting to be the canonical answer for this question (covers both parts): [Is there a php library for email address validation?](http://stackoverflow.com/questions/161342/is-there-a-php-library-for-email-address-validation) – Dan J Jun 03 '11 at 21:28
  • Does this answer your question? [Is there a php library for email address validation?](https://stackoverflow.com/questions/161342/is-there-a-php-library-for-email-address-validation) – peterh Nov 04 '20 at 11:37

7 Answers7

24

This is what I use:

   function check_email_address($email) {
        // First, we check that there's one @ symbol, and that the lengths are right
        if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
            // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
            return false;
        }
        // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
                return false;
            }
        }
        if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
                    return false;
                }
            }
        }

        return true;
    }

EDIT Replaced depreciated ereg with preg_match for PHP 5.3 compliance

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • Yes :( The OP didn't specify php 5.3. I still need to migrate this function to preg_match (which is trivial). It still works, though. – Chris Baker Jun 03 '11 at 21:32
  • :) All you should have to do is just add the delimiters and change the name. – Jim Jun 03 '11 at 21:33
  • 2
    Oh, I see... pressure me in to doing it now sort of thing. I see which way the wind blows. :P – Chris Baker Jun 03 '11 at 21:34
  • @Chris, why not php5 built in email verification : `return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email);`. Email address should be trimmed before the final testing. An integer casting of the return value is also necessary.Found here:electrictoolbox.com/php-email-validation-filter-var-updated. – Istiaque Ahmed Nov 21 '12 at 09:55
  • @IstiaqueAhmed at the time of writing, I felt that this strictly regex approach was the "best" for use cases that I've encountered. This is, of course, a very subjective view. `filter_var` works, and it uses a much more complex set of regular expressions than this. There's no specific reason not to use it, I simply prefer this approach because it has been reliable for me. Franky, I'm surprised he accepted the answer since I did not fully address his question :D – Chris Baker Nov 21 '12 at 18:27
  • @Chris, I had a wrong impression.Integer casting is necessary only for echoing purpose. – Istiaque Ahmed Nov 22 '12 at 04:11
2

If you really want to make sure its valid make your signup form send them an email with a URL link in that they have to click to validate.

This way not only do you know the address is valid (because the received the email), but you also know the owner of the account has signed up (unless someone else knows his login details).

To make sure it ends correctly you could use explode() on the '@' and check the second part.

$arr = explode('@', $email_address);
if ($arr[1] == 'mycollege.edu')
{
    // Then it's from your college
}

PHP also has it's own way of validating email addresses using filter_var: http://www.w3schools.com/php/filter_validate_email.asp

diggersworld
  • 12,770
  • 24
  • 84
  • 119
  • w3schools is **not a reliable source of information**. Use PHP's own documentation instead, seriously. http://www.php.net/manual/en/ref.filter.php Why trust "some website" when you can get wonderfully complete documentation straight from the source? – Chris Baker Jun 03 '11 at 21:45
  • Fair point about using PHP site as opposed to w3schools. It was the first thing that came up when I googled filter_var email. Will remember in future. – diggersworld Jun 03 '11 at 21:51
  • w3schools spends a lot of money to get themselves to the top of the results. They are also poisoning the web with faulty documentation on ASP, VB, C, and javascript (among others). They have no standing or credibility. For Javascript, put "MDC" in front of your Google search term and you'll get Mozilla Dev Center. For php, put "php.net" in front of your term to get the official docs every time. :) – Chris Baker Jun 03 '11 at 21:56
  • I'll probably end up sending a validation email address. Good idea – Ben Jun 03 '11 at 21:57
0

for any e-mail

([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?

for php preg_match function

/([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?/i

for @mycollege.edu

^([a-zA-Z0-9_-]+)(@mycollege.edu)$

for php preg_match function

/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i

PHP CODE

<?php 

$email = 'tahir_aS-adov@mycollege.edu';
preg_match('/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i', $email, $matches);

if ($matches) { 
    echo "Matched";
} else {
    echo "Not Matched";
}

var_dump($matches);
Janyk
  • 571
  • 3
  • 18
0

this might be a better solution. many answered already, eventhough its little different.

$email = "info@stakoverflow.com";

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
 echo $email ." is a valid email address";
} else {
  echo $email ." is not a valid email address";
}

I hope this one has simple to use.

0

A simple function using filter_var in php

<?php
function email_validation($email) {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
       echo("$email is a valid email address");
    } else {
       echo("$email is not a valid email address");
    }
}

//Test
email_validation('johnson123');
?>
0

This should work:

if (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])@mycollege.edu$/', $email)) {
     // Valid
}
gpresland
  • 1,690
  • 2
  • 20
  • 31
0

Read here http://ru2.php.net/manual/en/book.filter.php

Or in short

var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
azat
  • 3,545
  • 1
  • 28
  • 30