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

Joomla validate chrono forms using jquery

It is a common practice to use Chrono forms in our joomla site to setup various forms in the site, be it a contact us form, submit a ticket form, or whatever. I have set the option "validate form" to "No" under chronoform settings in admin panel, and also preferred to not include any js or css files. For validating the chrono forms, i prefer jquery. So first lets add jquery support in our joomla. It is quite simple: 1. Download jquery.js and jquery_min.js (1.4.2 version is enough) and place them in media/system/js folder of your joomla. 2. Edit libraries->joomla->html->html->behavior.php, and add the following function below the mootools() function: function jQuery($debug = null)     {         static $loaded;         global $mainframe;         // Only load once         if ($loaded) {    ...

Differences between php4 and php5

These are some of the differences i know between php4 and php5: 1. PHP5 supports more oops concepts like access specifiers and inheritance 2. we can pass objects by reference in php5 (using clone keyword) 3. Exception class included in PHP5 (try and catch) 4. More XML related functions like SimpleXML were included in PHP5 for easy  processing of XML data 5. PHP4 uses Zend engine 1.0 as a parser.PHP5 uses Zend engine 2.0 6. php 5 has improvements in design, security and stablity 7. Magic methods have been included in php5 8. Unified Constructors and Destructors in php5 9. There is a new error level defined as E_STRICT in php5  (notifies deprecated code) 10. Final keyword in php5 indicates that a method cannot be overridden by a  child Note: It is said that PHP5 has Backward compatibility issues with php4.

Facebook connect using graph api

An example of using facebook graph api to authenticate users: Setup a new application in facebook apps and get the app id and secret. Now follow the steps: Remember to give the same url in the below code, as that of the app settings. <?php if( !isset($_GET['code'])) { ?> <a href="https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxx&redirect_uri=http://xxxxxxxxxxxxxxxx/">Connect using Facebook</a> <?php } $code = trim($_GET['code']); if(isset($_GET['code'])) $token = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=xxxxxxxx&redirect_uri=http://xxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&code=$code"); $token = explode("=", $token); if(isset($_GET['code'])) $user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$token[1]));  print_r($user); ?>