发新话题
打印

完美的Email检测函数(正则匹配)

完美的Email检测函数(正则匹配)

完美的Email检测函数(正则匹配)

参考了"Yahoo"、
"Hotmail"、
"Gmail"、
"网易163邮箱"、
"网易126邮箱"、
"搜狐邮箱"、
"新浪邮箱"、
"TOM免费邮箱"、
"QQ邮箱"、
"亿邮"、
"21CN免费邮箱"等邮箱的用户名命名规则
结合域名构成规则
现写邮箱的检测函数如下,并含大量检测程序,以助说明,主要是正则匹配问题。
看了大量检测Email的正则匹配,没一个很完善的,包裹Discuz!



用户名命名规则开头必为数字或字母,中间可含"_"、"-"(搜狐邮箱)、"."(Gmail),长度32

域名构成规则
开头和结尾必为数字或字母,中间可含"-",长度32,最多4级域名,顶级域名又2至4个字母组成,(如:cn、com、name)


演示:http://retidy.com/email/checkEmail.php




代码:

<?php

/**
* Site checkEmail.php
*
* @author retidy <[email="retidy@163.com"]retidy@163.com[/email]>
* @version $Id 14时20分03秒 2005年12月11日 星期日 $
*
* @IP 219.145.62.91
* @Address 陕西省西安市 ADSL
*
*
*/


    function checkEmail($str)
    {
      if (eregi("^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$", $str))
        return true;
      else
        return false;  
    }

$str = [email="'retidy@retidy.com'"]'retidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 标准Email!\n<br><br>\n";

$str = [email="'retsdddddddddddddddddddddd3333333333dddddddddsddddddddddddddsdidy@retidy.com'"]'retsdddddddddddddddddddddd3333333333dddddddddsddddddddddddddsdidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名长!\n<br><br>\n";

$str = [email="'_retidy@retidy.com'"]'_retidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名开头必为字母或数字!\n<br><br>\n";

$str = [email="'.retidy@retidy.com'"]'.retidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名开头必为字母或数字!\n<br><br>\n";

$str = [email="'5retidy@retidy.com'"]'5retidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名开头必为字母或数字!\n<br><br>\n";

$str = 're\tidy@retidy.com';
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名只能由数字、字母、'.'、'-'和'_'组成!\n<br><br>\n";

$str = [email="'re#tidy@retidy.com'"]'re#tidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名只能由数字、字母、'.'、'-'和'_'组成!\n<br><br>\n";

$str = [email="'re/tidy@retidy.com'"]'re/tidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名只能由数字、字母、'.'、'-'和'_'组成!\n<br><br>\n";

$str = [email="'re.tidy@retidy.com'"]'re.tidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名只能由数字、字母、'.'、'-'和'_'组成!\n<br><br>\n";

$str = [email="'re-tidy@retidy.com'"]'re-tidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名只能由数字、字母、'.'、'-'和'_'组成!\n<br><br>\n";

$str = [email="'re_tidy@retidy.com'"]'re_tidy@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名只能由数字、字母、'.'、'-'和'_'组成!\n<br><br>\n";

$str = 'retidy\retidy.com';
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名和域名只能由一个[email="'@'"]'@'[/email]隔开!\n<br><br>\n";

$str = 'retidy#retidy.com';
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名和域名只能由一个[email="'@'"]'@'[/email]隔开!\n<br><br>\n";

$str = 'retidy/retidy.com';
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名和域名只能由一个[email="'@'"]'@'[/email]隔开!\n<br><br>\n";

$str = [email="'retidy@@retidy.com'"]'retidy@@retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 用户名和域名只能由一个[email="'@'"]'@'[/email]隔开!\n<br><br>\n";

$str = [email="'retidy@retidy4555555555555555222ssssssssssssffffffffffffffffffffffff.com'"]'retidy@retidy4555555555555555222ssssssssssssffffffffffffffffffffffff.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 网址长!\n<br><br>\n";

$str = [email="'retidy@retidy.retidy.retidy.com'"]'retidy@retidy.retidy.retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 最多三级域名!\n<br><br>\n";

$str = [email="'retidy@retidy.retidy.retidy.retidy.retidy.com'"]'retidy@retidy.retidy.retidy.retidy.retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 最多三级域名!\n<br><br>\n";

$str = [email="'retidy@retidy.comcn'"]'retidy@retidy.comcn'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 顶级域名最多4个字母!\n<br><br>\n";

$str = [email="'retidy@-retidy.com'"]'retidy@-retidy.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名开头或结尾为数字或字母!\n<br><br>\n";

$str = [email="'retidy@retidy-.com'"]'retidy@retidy-.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名开头或结尾必为数字或字母!\n<br><br>\n";

$str = [email="'retidy@163.com'"]'retidy@1\63.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名只能由数字、字母和'-'组成!\n<br><br>\n";

$str = [email="'retidy@1#63.com'"]'retidy@1#63.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名只能由数字、字母和'-'组成!\n<br><br>\n";

$str = [email="'retidy@1/63.com'"]'retidy@1/63.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名只能由数字、字母和'-'组成!\n<br><br>\n";

$str = [email="'retidy@1-63.com'"]'retidy@1-63.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名只能由数字、字母和'-'组成!\n<br><br>\n";

$str = [email="'retidy@1_63.com'"]'retidy@1_63.com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名只能由数字、字母和'-'组成!\n<br><br>\n";

$str = [email="'retidy@retidycom'"]'retidy@retidy\com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名间只能由一个'.'隔开!\n<br><br>\n";

$str = [email="'retidy@retidy#com'"]'retidy@retidy#com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名间只能由一个'.'隔开!\n<br><br>\n";

$str = [email="'retidy@retidy/com'"]'retidy@retidy/com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
echo " 域名间只能由一个'.'隔开!\n<br><br>\n";

$str = [email="'retidy@retidy..com'"]'retidy@retidy..com'[/email];
if(checkEmail($str) )
echo $str." is TRUE!\n<br>\n";
else
echo $str." is FALSE!\n<br>\n";
?>


演示:http://retidy.com/email/checkEmail.php
下载:http://retidy.com/email/checkEmail.zip



此外我还写了一个邮箱登陆程序,比hao123.com更强的邮箱登陆程序(一次可登陆多个邮箱),不同网站的多个邮箱,一次登完!
演示:http://retidy.com/email/
同时可以登多个邮箱,太方便了,原代码可以向我索取。



.      

TOP

【原创】Email检测函数

本人用javascript和VBscript b编写的Email检测代码,给大家参考一下!


<SCRIPT language=javascript>
function check()
{
  if(document.form1.email.value=="")
    {
      alert("email不能为空");
      return false;
    }
}
</SCRIPT>







<%
email=trim(Request("email"))
CheckEmail(email)
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<% public Function CheckEmail(str2)
    At(str2)
   Dian(str2)
   Shou(str2)
   YiChang(str2)
   WeiZhi(str2)
   dianhou(str2)
   dianruan(str2)
  end Function
  %>
  <% '判断"."
public Function Dian(str)
   str3= Len(trim(str))
    for i=1 to str3
       aa=mid(str,i,1)
    if aa="." then
    coun=coun+1
        end if
    if i=str3 then
     if coun<>1 then
      Response.Write "**********alert(""email好象没有 . 哦!"&Copyright&" "");location.href=""1.htm"";</script>"
    end if
   end if
  next
  end Function
  
   %>
   
   <% '判断"@"
   public Function  At(str)
     str3= Len(trim(str))
    for i=1 to str3
       aa=mid(str,i,1)
    if aa="@" then
    coun=coun+1
        end if
    if i=str3 then
     if coun<>1 then
      Response.Write "**********alert(""email好象不对哦!"&Copyright&" "");location.href=""1.htm"";</script>"
    end if
   end if
  next
   end Function %>
   <% '判断首字符
     public Function  Shou(str)
  aa=left(str,1)
      if asc(aa) <=48 or  asc(aa)>=57 and  asc(aa)<65  or   asc(aa)>=91 and asc(aa)<=96 or  asc(aa)>=123 then
    Response.Write "**********alert(""首字符必须是字符或数字!"&Copyright&" "");location.href=""1.htm"";</script>"
    end if
     end Function
     %>
   <%
     public Function   YiChang(str)                   '判断 异常字符
dim bb
  str3= Len(trim(str))
    for i=1 to str3
       aa=mid(str,i,1)
    if asc(aa)>=48 and asc(aa)<=57 or asc(aa)>=65 and asc(aa)<=90 or asc(aa)>=97 and asc(aa)<=122or  aa="_" or aa="@"or aa="." then
    bb =1
else
    Response.Write "**********alert("" 不能有乱玛!"&Copyright&" "");location.href=""1.htm"";</script>"
    end if
  next
end Function
     %>
    <% '判断位置
     public Function  WeiZhi(str)
  dim x,y
  str3= Len(trim(str))
    for i=1 to str3
       aa=mid(str,i,1)
  If  aa="@" Then
    x=i
  End If
If  aa="." Then
    y=i
  End If
  next
   if   y-x<=2 then
    Response.Write "**********alert("" @ 和.最少隔开两个字符!"&Copyright&" "");location.href=""1.htm"";</script>"
  end if
end Function
   %>
   <% '判断点后面是否有字
     public Function  dianhou(str)
  str3= Len(trim(str))
    i=instr(str,".")
  If  i=str3 Then
   Response.Write "**********alert("" 格式不对!"&Copyright&" "");location.href=""1.htm"";</script>"
  End If

    for x=i to str3
       aa=mid(str,x,1)
    if asc(aa)>=48 and asc(aa)<=57  then
    Response.Write "**********alert("" 末尾不能为数字!"&Copyright&" "");location.href=""1.htm"";</script>"
    end if
  next
end Function
   %>
     <% '判断点和@中间的"_",点后面"_"
     public Function  dianruan(str)
  str3= Len(trim(str))
    i=instr(str,".")
j=instr(str,"@")
for X=j+1 to i-1
       aa=mid(str,X,1)
    if aa="_" then
   Response.Write "**********alert(""格式不对!"&Copyright&" "");location.href=""1.htm"";</script>"
        end if
    next
   for X=i+1 to str3
       aa=mid(str,X,1)
    if aa="_" then
   Response.Write "**********alert(""格式不对!"&Copyright&" "");location.href=""1.htm"";</script>"
        end if
   next
end Function
   %>      

TOP

发新话题