通过用户名验证进行电子邮件验证,仅接受字母,数字,下划线和DOT

时间:2015-04-08 13:37:51

标签: php regex

我知道这个问题很多次,但我找不到任何解决方案来进行具体验证。

我想验证电子邮件地址中的用户名,只接受字母,数字,下划线和DOT和NO破折号( - )或任何特殊字符,例如!#%& *()

像这样:aaa @ aa.com,d123 @ ad.com,22_dd @ dd.com,dfd.df @ ds.com

不是这样的:ss-ee @ sd.com,fsd!@ atd.com,11-ee @ sd.com

我做了什么:

if (!preg_match("/[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]+/", $email)) return("Invalid email address"); 

但它接受破折号。

4 个答案:

答案 0 :(得分:1)

你可以这样做:

if (!preg_match('/^(?!.*?\.\.)[\w.]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]+$/', $email)) 
    return("Invalid email address"); 

答案 1 :(得分:1)

要防止连续2个点(.)使用negative lookahead (?!.*?\.\.)

if (!preg_match('/^(?!.*?\.\.)[a-z0-9_.]+@[a-z0-9.-]+\.[a-z]+$/im', $email))
    return("Invalid email address"); 

说明:

^             # Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed)
(?!           # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
   .             # Match any single character that is NOT a line break character (line feed)
      *?            # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
   \.            # Match the character “.” literally
   \.            # Match the character “.” literally
)
[a-z0-9_.]    # Match a single character present in the list below
                 # A character in the range between “a” and “z” (case insensitive)
                 # A character in the range between “0” and “9”
                 # A single character from the list “_.”
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
@             # Match the character “@” literally
[A-Z0-9.-]    # Match a single character present in the list below
                 # A character in the range between “A” and “Z” (case insensitive)
                 # A character in the range between “0” and “9”
                 # The literal character “.”
                 # The literal character “-”
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\.            # Match the character “.” literally
[A-Z]         # Match a single character in the range between “A” and “Z” (case insensitive)
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\$             # Assert position at the end of a line (at the end of the string or before a line break character) (line feed)

答案 2 :(得分:0)

您可以使用以下正则表达式,这也会阻止用户名中的2个连续点:

^(?!.*\.\..*@)[a-zA-Z0-9_.]+@\S+\.[a-zA-Z]+$

请参阅demo here

<强>详情

  • ^ - 字符串开头
  • (?!.*\.\..*@) - 如果仅在用户名部分中有两个连续的点,则表示匹配失败的否定前瞻
  • [a-zA-Z0-9_.]+ - 一个或多个ASCII字母,数字或_.
  • @ - @
  • \S+ - 1 +非空白字符
  • \. - 一个点
  • [a-zA-Z]+ - 一个或多个ASCII字母
  • $ - 字符串结束。

示例代码

$re = "/^(?!.*\\.\\..*@)[a-zA-Z0-9_.]+@\\S+\\.[a-zA-Z]+$/m"; 
$str = "334345jtjert..j547j@ds.com\n334345jtjert.j547j@ds.com\nss-ee@sd.com\nfsd!@asd.com\n11-ee@sd.com"; 
preg_match($re, $str, $matches);

答案 3 :(得分:-2)

您可以使用:

[\\w\\-\\+\\&\\*]+(?:\\.[\\w\\-\\_\\+\\&\\*]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}