英文:
Apache commons email validator: allow plus character in e-mail
问题
使用 org.apache.commons.validator.routines.EmailValidator(1.7)来进行Java中的电子邮件验证。
在GMAIL中,用户可以使用 myname+anything@gmail.com
,它将发送到 myname@gmail.com
。然而,EmailValidator
不允许 +
字符。我还尝试在验证之前将 +
替换为 %2b
,但也失败了。
有什么建议吗?我可以在我的 EmailValidator 实例中添加一个额外允许的字符吗?我找不到这个选项,但我猜我需要类似的东西。
附注:由于各种原因,我不太想使用自定义正则表达式。
英文:
using org.apache.commons.validator.routines.EmailValidator (1.7) for e-mail validation in Java.
In GMAIL a user is allowed to use myname+anything@gmail.com
and it will go to myname@gmail.com
. However, the EmailValidator
does not allow the +
character. I also tried replacing the +
with %2b
before validation but it also fails.
Any suggestions? Can I add an extra allowed character to my EmailValidator instance? Couldn't find this but guess I would need something like that.
Ps. I don't really want to go to custom regex for various reasons.
答案1
得分: 2
The Apache Commons EmailValidator 在处理加号时确实有效。输出结果为:true。
英文:
The Apache Commons EmailValidator does work with a plus sign.
import org.apache.commons.validator.routines.EmailValidator;
public class MapperMain {
public static void main(String[] args) {
String email = "john+Williams@ennui.co.za";
EmailValidator emailValidator = EmailValidator.getInstance();
System.out.println(emailValidator.isValid(email));
}
}
Outputs:
true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论