2

我正在使用 Ryan J. Heldt http://validation.riaforge.org/的出色验证 CFC

但电子邮件验证 RE 有问题。RFC 5322 允许以下字符

! # $ % & ' * + - / = ? ^ _ ` { | } ~

但是 validate.cfc 中的 RE 拒绝 JohnO'Connell@somewhere.com 因为撇号。

有问题的 RE 位于以下代码块中

<cffunction name="validateEmail" returntype="void" access="private" output="false">
    <cfargument name="parameters" type="string" required="true" />
    <cfset var rr = 0 />
    <cfloop index="rr" list="#arguments.parameters#" delimiters=";">
        <cfif isDefined("#listGetAt(rr,1,"|")#") and len(_fields[listGetAt(rr,1,"|")]) and not reFind("^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$",_fields[listGetAt(rr,1,"|")])>
            <cfset registerError(listGetAt(rr,1,"|"),listGetAt(rr,2,"|")) />
        </cfif>
    </cfloop>
    <cfreturn />
</cffunction>

我对 RE 的了解不足以提出解决方案,尽管我已将此通知 Ryan(以及一年前的另一个错误),但他似乎并未处于错误修复模式。

任何人都可以建议一个替代的正则表达式吗?

4

3 回答 3

1

I'll take a stab at updating the RegEx to allow those special characters in the name, but as a general rule of thumb I have very loose validation on email addresses; because seemingly nobody implements them according to spec. My validation usually consists of:

  • contains '@'
  • contains 1+ characters before '@'
  • contains 3+ characters after '@'
  • 1+ characters after '@' must be '.'

While this allows for a lot of false positives to slip through, it also won't create any false negatives.

I'm not going to try to update that regex to spec as it's nowhere near complex enough to match the spec exactly. If you just want to allow special characters in the name, then use this:

and not reFind("^[a-zA-Z][\w\.\##\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
于 2010-08-09T17:15:54.790 回答
1

这是我典型的电子邮件正则表达式:

^['_a-zA-Z0-9-\+~]+(\.['_a-zA-Z0-9-\+~]+)*@([a-zA-Z_0-9-]+\.)+(([a-zA-Z]{2})|(aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel))$
于 2010-08-09T17:33:11.207 回答
0

你用的是什么版本的CF?从 CF8 开始,您可以使用 IsValid() 来检查电子邮件:

<cfset myemail = "me@exampl.ecom">
<cfoutput>#IsValid("email", myemail)#</cfoutput>
于 2010-08-09T17:47:03.263 回答