PHP Email Validation

How to validate an Email in PHP?

You can use the filter_var() function, which gives you a lot of handy validation and sanitization options.

filter_var($email, FILTER_VALIDATE_EMAIL)
  • PHP Manual filter_var()

  • Available in PHP >= 5.2.0

If you don't want to change your code that relied on your function, just do:

function isValidEmail($email){ 
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

Note: For other uses (where you need Regex), the deprecated ereg function family (POSIX Regex Functions) should be replaced by the preg family (PCRE Regex Functions). There are a small amount of differences, reading the Manual should suffice.

Update 1: As pointed out by @binaryLV:

PHP 5.3.3 and 5.2.14 had a bug related to
FILTER_VALIDATE_EMAIL, which resulted in segfault when validating
large values. Simple and safe workaround for this is using strlen()
before filter_var(). I'm not sure about 5.3.4 final, but it is
written that some 5.3.4-snapshot versions also were affected.

This bug has already been fixed.

Update 2: This method will of course validate bazmega@kapa as a valid email address, because in fact it is a valid email address. But most of the time on the Internet, you also want the email address to have a TLD: bazmega@kapa.com. As suggested in this blog post (link posted by @Istiaque Ahmed), you can augment filter_var() with a regex that will check for the existence of a dot in the domain part (will not check for a valid TLD though):

function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL)
&& preg_match('/@.+\./', $email);
}

As @Eliseo Ocampos pointed out, this problem only exists before PHP 5.3, in that version they changed the regex and now it does this check, so you do not have to.

PHP email validation

I suggest you use the FILTER_VALIDATE_EMAIL filter:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//valid
}

You can also use its regular expression directly:

"/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD"

But in that case, if a bug is found in the regular expression, you'll have to update your program instead of just updating PHP.

Worldwide email validation

In PHP you can use filter_var. Example from the docs:

$email = filter_var('bob@example.com', FILTER_VALIDATE_EMAIL);

In this case, $email will contain "bob@example.com". For an invalid email address, it will contain false.

Email Validation php

You'll have to dig into preg_match() function.

Searches subject for a match to the regular expression given in pattern.

For example :

$email = "x12627353@student.ncirl.ie";
$result = preg_match('/^x[\d]{8}@student\.ncirl\.ie$/', $email);

if ($result) {
...
do some stuff
...
}

The test of the regular expression on regex101.

The explanations :

^ asserts position at start of the string

x matches the character x literally (case sensitive)

Match a single character present in the list below :

[\d]{8}

{8} Quantifier — Matches exactly 8 times

\d matches a digit (equal to [0-9])

@student matches the characters @student literally (case sensitive)

. matches the character . literally (case sensitive)

ncirl matches the characters ncirl literally (case sensitive)

. matches the character . literally (case sensitive)

ie matches the characters ie literally (case sensitive)

$ asserts position at the end of the string, or before the line
terminator right at the end of the string (if any)

Hope it helps.

PHP email validation function

You can use filter_var to validate the e-mail address:

if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
// invalid e-mail address
}

Simple php email validation

You should only write to the text file when the validation has succeeded, like this:

<?php
if (isset($_POST['submit'])) {
$email = $_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

echo 'Du er nu tilmelds nyhedsbrevet';
$file = fopen("emaillist.txt","a+");
fwrite($file,$email . "\n");
fclose($file);
// print_r(error_get_last());

} else {

echo 'Du har indtastet en ugyldig email';

}

}

?>

PHP: Email validation and accepting email address from a certain domain

Php has an easy function to help you with checking if an email address is valid:

$isValid = filter_var($email, FILTER_VALIDATE_EMAIL);

To check if the email adress is a gmail address, the following would do the trick:

list ($user, $domain) = explode('@', $email);
$isGmail = ($domain == 'gmail.com');


Related Topics



Leave a reply



Submit