简体   繁体   English

Java / Spring MVC 3验证电子邮件地址

[英]Java / Spring MVC 3 validation of an email address

I have a Java backend with Spring MVC and I am using validation in this way on my domain object for an email address: 我有一个带有Spring MVC的Java后端,我在我的域对象上以这种方式使用验证来获取电子邮件地址:

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
...
@NotNull
@Size(min = 1, max = 100)
@Pattern(regexp="^([a-zA-Z0-9\\-\\.\\_]+)'+'(\\@)([a-zA-Z0-9\\-\\.]+)'+'(\\.)([a-zA-Z]{2,4})$")
private String email;

But all I get with these lines of code 但是我只能使用这些代码行

Set<ConstraintViolation<Person>> failures = validator.validate(personObject);
...
Map<String, String> failureMessages = new HashMap<String, String>();
for (ConstraintViolation<Person> failure : failures) {
    failureMessages.put(failure.getPropertyPath().toString(), failure.getMessage());
    System.out.println(failure.getPropertyPath().toString()+" - "+failure.getMessage())
}

I get this on the console: 我在控制台上得到这个:

email - must match "^([a-zA-Z0-9\\-\\.\\_]+)'+'(\\@)([a-zA-Z0-9\\-\\.]+)'+'(\\.)([a-zA-Z]{2,4})$"

but I have as email address test@test.com , so the regexp does not match. 但我的电子邮件地址为test@test.com ,因此正则表达式不匹配。

So I have two prolems: 所以我有两个问题:

  • What's wrong here? 这有什么不对?
  • And how can I define a error message on my own, because display this to the user, that is not a good thing :-) 我怎么能自己定义一个错误信息,因为这个显示给用户,这不是一件好事:-)

Thank you in advance for your help and Best Regards. 提前感谢您的帮助和最诚挚的问候。

如果使用Hibernate Validator ,则可以使用@Email注释无论如何,您可以创建自定义约束注释并设置要在资源属性文件中显示的自定义消息。

First try simpler regex such as this: 首先尝试更简单的正则表达式,例如:

"\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"

Than you can try RFC 2822 version: 比你可以尝试RFC 2822版本:

"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"

Let me know if the either worked for you. 如果这两个都适合你,请告诉我。

Also take look at this package 另外看看这个包

org.springmodules.validation.bean.conf.loader.annotation.handler

here 这里
http://www.springbyexample.org/examples/spring-modules-validation-module.html http://www.springbyexample.org/examples/spring-modules-validation-module.html

It might be better alternative. 这可能是更好的选择。

You can use @Email from Hibernate Validator : 您可以使用Hibernate Validator中的 @Email

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.4.1.Final</version>
    </dependency>

Your regex has a couple of instances of '+' in it, which is kind of odd. 你的正则表达式中有几个'+'实例,这有点奇怪。 e-mail addresses aren't usually required to have single quotes in them :) I think perhaps that is meant to be concatenating pieces of the String, and those should be double quotes? 电子邮件地址通常不需要在其中包含单引号:)我想也许这可能是连接字符串的部分,那些应该是双引号?

For defining your own message, you just add message="{someWay.of.definingCodes}" to the annotation. 要定义自己的消息,只需在注释中添加message="{someWay.of.definingCodes}"即可。 Then define a translation for it in ValidationMessages.properties in the default package. 然后在默认包中的ValidationMessages.properties中为其定义转换。

Alternately hibernate validator provides org.hibernate.validator.Email if you're willing to depend on a vendor extension. 或者,如果您愿意依赖供应商扩展,则hibernate验证器会提供org.hibernate.validator.Email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM