电子邮件验证和用户名验证只接受字母、数字、下划线和DOT


Email validation with validation on username to accept only letters, digits, underscore and DOT

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

我想验证电子邮件地址中的用户名,使其仅接受字母、数字、下划线和DOT以及无短划线(-)或任何特殊字符,如!#%&()

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

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

我做了什么:

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

但它接受dash。

你可以做:

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

为了防止2个连续点(.),使用负前瞻(?!.*?'.'.)

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个连续点:

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

请参阅此处的演示。

详细信息

  • ^-字符串的开头
  • (?!.*'.'..*@)-如果用户名部分只有两个连续的点,则会导致匹配失败
  • [a-zA-Z0-9_.]+-一个或多个ASCII字母、数字或_.
  • @-一个@
  • 'S+-1+非空白字符
  • '.-一个点
  • [a-zA-Z]+-1个或多个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);

您可以使用这个:

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