本文介绍下,在php中验证Email邮箱地址格式的一个例子,有需要的朋友参考下。
在php代码中,经常会遇到验证邮箱格式是否正确的情况,本节分享一段入门级的参考代码,教大家一些基础的验证方法。 代码: <html> <head> <title>php Email验证的演示示例-bbs.it-home.org</title> </head> <body> <form name="form1" method="post" action="EMailValidation.php"> <table> <tr> <td colspan="2"><b>请输入一个Email地址</div></td> </tr> <tr> <td width="25%">E-mail</td> <td width="75%"> <input type="text" name="email" size="30"> </td> </tr> <tr> <td colspan="2"> <input type="submit" name="Submit" value="Submit"> </td> </tr> </table> </form> </body> </html> <!-- EMailValidation.php <html> <head> <title> 验证Email地址 </title> </head> <body> <?php if (!isset($email)){ die("没有检测到任何的Email地址数据"); } if(empty($email)) { die("您输入的内容为空"); } if ( (strlen($email) < 3) || (strlen($email) > 200)) { die("Email长度不符合要求"); } elseif(!ereg("@",$email)) { die("Invalid E-mail address, no @ symbol found"); } else { echo "<b>".$email."</b> is correct in format.<br>"; } list($username,$hostname) = split("@",$email); if ( (empty($username)) or (empty($hostname)) ) { die("username or host name section is not valid."); } if(checkdnsrr($hostname)) { echo "<b>". $email ."</b> hostname has a valid MX record !<br>"; } else { die("<b>". $email ."</b> hostname does not exist"); } ?> <br> </body> </html> --> |