Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

validating an emailadres

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

validating an emailadres

Postby DoppyNL » Thu Oct 03, 2002 3:36 am

anyone got any code to validate a emailadres wich was entered by a user?
probably a short bit of code wich somebody has allready made.
saves me a lot of time :)

tnx,

Greetz Daan
DoppyNL
 

Postby qadeerbk » Fri Oct 04, 2002 12:13 am

//**************************************
//
// Name: Email Validator
// Description:Email Validator check to
// see if an email address is in the proper
// format or not.
// By: Bhushan Paranjpe
//
//This code is copyrighted and has // limited warranties.Please see http://
// http://www.Planet-Source-Code.com/xq/ASP/txtCod
// eId.758/lngWId.8/qx/vb/scripts/ShowCode.
// htm //for details. //**************************************
//
<?php
function emailsyntax_is_valid($email) {
list($local, $domain) = explode("@", $email);
$pattern_local = '^([0-9a-z]*([-|_]?[0-9a-z]+)*)(([-|_]?)\.([-|_]?)[0-9a-z]*([-|_]?[0-9a-z]+)+)*([-|_]?)$';
$pattern_domain = '^([0-9a-z]+([-]?[0-9a-z]+)*)(([-]?)\.([-]?)[0-9a-z]*([-]?[0-9a-z]+)+)*\.[a-z]{2,4}$';
$match_local = eregi($pattern_local, $local);
$match_domain = eregi($pattern_domain, $domain);

if ($match_local && $match_domain) {
return 1;
} else {
return 0;
}
}
?>
Use the function like this :
if (emailsyntax_is_valid($email)) {
echo "Ok";
} else {
echo "Error";
}
qadeerbk
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sun Sep 29, 2002 9:36 am

Postby DoppyNL » Sat Oct 05, 2002 9:12 am

It seems to be working, allthough I'm to lazy to test it completely :)
I can't see what is exactly valid and what is not, but hey, who cares, as long as the really wrong entry's are filtered out :D

tnx.

Greetz Daan
DoppyNL
 

Postby Jay » Sat Oct 05, 2002 10:01 am

There is a (free) product called PHORM which, if you dissect it and understand it, has a very advanced email validation code!

It actually does it in 5 steps, and you can choose how in depth you want to check it!
1 - Check the string is formatted correctly for an email address
2 - Checks the domain ext (.com .co.uk) is a valid one
3 - Checks the domain exists (ie does microsoft.com exist)
4 - Checks that the mailbox actually exists for that domain (ie does bill.gates@microsoft.com actually exist)
5 - Oh Sh*te, I've missed one out somewhere, but you get the idea!
Jay
 

Postby Jay » Sun Oct 06, 2002 11:09 am

Oh well, I went and found it for you:

Code: Select all
  function MailChek($Addr, $Level, $Timeout = 15000) {

//  Valid Top-Level Domains
    $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:";
    $CCs   = "ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:".
             "bg:bh:bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:".
             "ck:cl:cm:cn:co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:".
             "eh:er:es:et:fi:fj:fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:".
             "gp:gq:gr:gs:gt:gu:gw:gy:hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:".
             "is:it:jm:jo:jp:ke:kg:kh:ki:km:kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:".
             "lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:ml:mm:mn:mo:mp:mq:mr:ms:mt:".
             "mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:np:nr:nt:nu:nz:om:pa:".
             "pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:rw:sa:sb:sc:sd:".
             "se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:tf:tg:th:".
             "tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:ve:".
             "vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:";

//  The countries can have their own 'TLDs', e.g. mydomain.com.au
    $cTLDs = "com:net:org:edu:gov:mil:co:ne:or:ed:go:mi:aero:biz:coop:info:museum:name:";

    $fail = 0;

//  Shift the address to lowercase to simplify checking and trim
    $Addr = trim(strtolower($Addr));

//  Check for spaces
    if (ereg(" ", $Addr)) $fail = 1;

//  Split the Address into user and domain parts
    $UD = explode("@", $Addr);
    if (sizeof($UD) != 2 || !$UD[0]) $fail = 1;

//  Split the domain part into its Levels
    $Levels = explode(".", $UD[1]); $sLevels = sizeof($Levels);
    if (!$Levels[0] || !$Levels[1]) $fail = 1;

//  Get the TLD, strip off trailing ] } ) > and check the length
    $tld = $Levels[$sLevels-1];
    $tld = ereg_replace("[>)}]$|]$", "", $tld);
    if (strlen($tld) < 2
    || (strlen($tld) > 3 && !ereg(":$tld:", ":arpa:aero:coop:info:museum:name:"))) $fail = 1;

    $Level--;

//  If the string after the last dot isn't in the generic TLDs or country codes, it's invalid.
    if ($Level && !$fail) {
    $Level--;
    if (!ereg($tld.":", $gTLDs) && !ereg($tld.":", $CCs)) $fail = 2;
    }

//  If it's a country code, check for a country TLD; add on the domain name.
    if ($Level && !$fail) {
    $cd = $sLevels - 2; $domain = $Levels[$cd].".".$tld;
    if (ereg($Levels[$cd].":", $cTLDs)) { $cd--; $domain = $Levels[$cd].".".$domain; }
    }

//  See if there's an MX record for the domain
    if ($Level && !$fail) {
    $Level--;
    if (!getmxrr($domain, $mxhosts, $weight)) $fail = 3;
    }

//  Attempt to connect to port 25 on an MX host
    if ($Level && !$fail) {
    $Level--;
    while (!$sh && list($nul, $mxhost) = each($mxhosts))
      $sh = fsockopen($mxhost, 25);
    if (!$sh) $fail=4;
    }

//  See if anyone answers
    if ($Level && !$fail) {
    $Level--;
    set_socket_blocking($sh, false);
    $out = ""; $t = 0;
    while ($Timeout-- && !$out)
      $out = fgets($sh, 256);
    if (!ereg("^220", $out)) $fail = 5;
    }

    if ($sh) fclose($sh);

    return $fail;
  } //MailChek
Jay
 


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.