Apache Commons Email验证器:允许电子邮件中的加号字符

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月14日 17:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定