Validate email addresses using PHP

Validate E-Mail Addresses Using PHP
This PHP function validates an email address without using RegEx. It's not the most comprehensive function however, as it doesn't check for special characters that are not allowed in an email address.
1. <?
2. // Validates email addresses without using RegEx
3. function ValidateEmail($EmailAddr)
4. {
5.        $At = "@";     
6.        $EmailArr = split ($At,$EmailAddr);            
7.        $EmailAddr = trim($EmailAddr);
8.        $EmailLen = strlen($EmailAddr);
9.        // Find the dot
10.        $HasDot = 0;
11.        for($i = 2; $i <= $EmailLen; $i++)
12.        {
13.                $j = substr($EmailAddr, 0, $i);
14.                $jLen = strlen($j);                    
15.                $LastJ = substr($j, $jLen - 1, $jLen);                 
16.                $GotDot = ord($LastJ);                         
17.                if($GotDot == 46)
18.                {                              
19.                        $HasDot = 1;
20.                }                                              
21.        }       
22.        // Find the space
23.        $HasSpace = 0;
24.        for($n=0; $n < $EmailLen; $n++)
25.        {
26.                $GotSpace = substr($EmailAddr, $n, 1);                 
27.                if(ord($GotSpace) == 32)
28.                {
29.                        $HasSpace = 1;
30.                }
31.               
32.        }                              
33.        if($EmailArr[1])
34.        {
35.                $HasAt = 1;
36.        }              
37.        if($HasAt == 1 && $HasDot == 1 && $HasSpace == 0)
38.        {
39.                $EmailIsValid = 1;
40.        }
41.        else
42.        {
43.                $EmailIsValid = 0;
44.        }              
45.        return ($EmailIsValid);        
46.  }              
47. 
48.  // How to use the email address validator
49. $EmailAddrAddr = "[email protected]";
50. if(ValidateEmail($EmailAddrAddr) == 1)
51. {
52.        echo $EmailAddrAddr." is a valid email address.";
53. }
54. else
55. {
56.        echo $EmailAddrAddr." is not a valid email address.";
57.  }
58.  ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top