【问题标题】:Struts, email validation in ActionForm validate ()Struts、ActionForm中的邮件验证 validate()
【发布时间】:2013-07-29 15:15:07
【问题描述】:

我的页面包含电子邮件输入。我的目标是在服务器端进行验证。 现在我的验证码如下:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

            if(this.name == null || this.name.trim().equals(""))
                errors.add("name", new ActionMessage("errors.required","Name"));
            if(this.email == null || this.email.trim().equals(""))
                errors.add("email", new ActionMessage("errors.required","Email"));
            if(this.city == null || this.city.trim().equals(""))
                errors.add("city", new ActionMessage("errors.required","City"));           
               if(this.country == null || this.country.trim().equals(""))
                errors.add("country", new ActionMessage("errors.required","Country")); 
                return errors;   
    }

我知道我可以在 validate 方法中使用正则表达式进行电子邮件验证。在 Struts 提供的验证方法中,是否有任何不同的方法来验证电子邮件、ip 等字段?但是有一个限制,即验证必须严格在 validate 方法中。谢谢

【问题讨论】:

  • 不相关,但我真的会考虑使用StringUtils.isBlank(xyz) 之类的东西,或者自己编写以避免大量嘈杂、毫无意义的重复。

标签: validation struts


【解决方案1】:

commons-validator 可能会帮助您实现您想要做的事情。

验证:

  • 电子邮件:org.apache.commons.validator.EmailValidator
  • ip:org.apache.commons.validator.routines.InetAddressValidator
  • ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多