Struts,ActionForm中的电子邮件验证validate()



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

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等字段?但由于验证方法中必须严格进行验证这一限制。感谢

commons验证器可以帮助您实现您想要做的事情。

验证:

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

最新更新