Skip to main content

Validate Email


<?php

function smcf_validate_email($email) {

$at = strrpos($email, "@");


// Make sure the at (@) sybmol exists and

// it is not the first or last character

if ($at && ($at < 1 || ($at + 1) == strlen($email)))

return false;


// Make sure there aren't multiple periods together

if (preg_match("/(\.{2,})/", $email))

return false;


// Break up the local and domain portions

$local = substr($email, 0, $at);

$domain = substr($email, $at + 1);


// echo $local.'<br>';

// echo $domain.'<br>';


// Check lengths

$locLen = strlen($local);

$domLen = strlen($domain);

if ($locLen < 1 || $locLen > 64 || $domLen < 4 || $domLen > 255)

return false;


// Make sure local and domain don't start with or end with a period

if (preg_match("/(^\.|\.$)/", $local) || preg_match("/(^\.|\.$)/", $domain))

return false;


// Check for quoted-string addresses

// Since almost anything is allowed in a quoted-string address,

// we're just going to let them go through

if (!preg_match('/^"(.+)"$/', $local)) {

// It's a dot-string address...check for valid characters

if (!preg_match('/^[-a-zA-Z0-9!#$%*\/?|^{}`~&\'+=_\.]*$/', $local))

return false;

}


// Make sure domain contains only valid characters and at least one period

if (!preg_match("/^[-a-zA-Z0-9\.]*$/", $domain) || !strpos($domain, "."))

return false;


// Check if the length of last part is atleast 2

$last_part = explode(".", $domain);

// echo strlen($last_part[1]);

if(strlen($last_part[1]) <= 1)

return false;


return true;

}


$email = 'dskanth@gmail.com';

if (!smcf_validate_email($email))

echo 'Invalid email';

else

echo 'valid email';

?>

Comments

  1. Another simple email validation check:

    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
    {
    echo $email.' is not a valid email';
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

php strip all tags

The below function can strip almost all tags from a string. function strip_all_tags($string) {     $string = preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $string );     $string = strip_tags($string);     return trim($string); } $a = '<script type="text/javascript" src="jquery.js"></script> <div id="test" style="padding:5px; color:red;">Hello world</div>'; echo strip_all_tags($a); // outputs: Hello world

htaccess disable directory browsing, disable file listing, disable file access

There may be some personal or secure data in a file, that you want to hide from the end user, when it is viewed through a web browser. Ex: http://www.example.com/user_files/24/chat.txt In the above scenario, if the file "chat.txt" contains a secure data, and you dont want to allow end users to directly access the file, or to disable the file listing in the folder "user_files" or "24", use the below code in your .htaccess file inside "user_files" folder: Options -Indexes order allow,deny deny from all

css link remove dotted outline

Its quite awkward to look at the link, when it is clicked and the dotted outline appears on it. To overcome it, just add the following line to your css file: a { outline: 0; } For example, to remove the dotted outline for the menu links on your page, add this to your css: #navigation li a {    outline: 0; } ( Assuming you have this menu structure: <ul id="navigation"><li><a href="news.html">News</a></li></ul> )