View Full Version : Php email validation
keyboard
06-06-2012, 07:45 AM
Hello All,
I'm looking for a way to validate an email input ($email). Preferably like this -
if(!validemail) {
echo 'Please enter a valid email<br />';
$check = 'false';
}
I need it to validate all valid emails (I believe emails can have % signs in them and so on)
Thanks, Keyboard1333
ApacheTech
06-06-2012, 09:54 AM
Enjoy. :D
<?php
/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email
address format and the domain exists.
*/
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
?>
keyboard
06-06-2012, 10:43 AM
This part's making an error-
if ($isValid && !(checkdnsrr($domain,"MX") ||
↪checkdnsrr($domain,"A")))
ApacheTech
06-06-2012, 11:01 AM
Try:
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
Edited in above post.
keyboard
06-06-2012, 11:27 AM
Thanks, I changed that and it fixed the error.
How do I actually validate with the code? (I never really use functions)
why not
<?php
function checkemail( $email ){
return filter_var( $email,FILTER_VALIDATE_EMAIL );
}filter_var uses this regex:
/^(?!(?:(?:\\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
which is fairly complete (http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?revision=320369&view=markup). it doesn't check domain name records as Apache's function does, however, note that just because a domain name exists doesn't mean that the email address does too.
In the end, the only way to confirm that an email really exists is to send an email to it and see if it bounces.
Also consider that many DNS servers (google and opendns included) drop record check requests that they think are automated - so checkdnsrr() actually fails more often than you might think.
ApacheTech
06-06-2012, 05:18 PM
That code is from a 12 page guide on how to fully validate emails via PHP. One of the first links in a simple search for "php email address validation". :D
Usage:
<?php
if (!validEmail($email)) {
// Do something if email is not valid.
} else {
// Do something is email is valid.
}
?>
djr33
06-06-2012, 05:45 PM
But as traq pointed out (a little indirectly) there are problems with that script, and clearly it's more work than just using PHP's built in function for it, as shown in his post.
ApacheTech
06-06-2012, 06:16 PM
I had no idea there was such a function inbuilt into php, lol. Thank you to Traq for the lesson. :D
That code is from a 12 page guide on how to fully validate emails via PHP. One of the first links in a simple search for "php email address validation". :D
Right - I'm not saying it won't work, or that it is in any way "bad" (though the DNS record check will probably only get more problematic over time). Just that there are much simpler/quicker ways to get similar results.
keyboard
06-06-2012, 09:34 PM
Thanks for your suggestion traq!
So, how do I actually use it?
Like this -
if(checkemail($email)) {
echo 'yes';
} else {
echo 'no';
}
? I'm sorry, but I've never really used php functions...
yeah, though you probably won't be echoing anything right then. Maybe more like
// . . .
if( !checkemail( $_POST['useremail'] ) ){
/* throw an error */
}
// . . .
keyboard
06-06-2012, 10:41 PM
Thanks for that traq! (the echo was just to try and tell if I was doing it right :D)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.