logo

Weblog

Check an email address in jstl

I was looking for a way to validate email addresses in jstl, or rather in a jspx template, and used org.apache.commons.validator for that purpose.

Thu 17 Oct 2013 / André / 0 comments

You need to download and install the commons-validator jar first to make it available to your web application.

This short example uses the org.apache.commons.validator.routines.EmailValidator class.

<jsp:root 
 xmlns:jsp="http://java.sun.com/JSP/Page"
 xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns="http://www.w3.org/1999/xhtml"
  version="2.0">
<jsp:directive.page
    import="org.apache.commons.validator.routines.*" />

<jsp:root
  xmlns:jsp="http://java.sun.com/JSP/Page"
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns="http://www.w3.org/1999/xhtml"
  version="2.0">

  <jsp:directive.page
    import="org.apache.commons.validator.routines.*" />

  <jsp:scriptlet>
    String email = request.getParameter("email"); 
    Boolean valid = false;
    if (EmailValidator.getInstance().isValid(email)) {
        valid = true;
    }
    pageContext.setAttribute("valid", valid);
  </jsp:scriptlet>

  <c:choose>
    <c:when test="${valid}">
      <em>${param.email} is valid.</em>
    </c:when>
    <c:otherwise>
      <em>${param.email} is NOT valid.</em>
    </c:otherwise>
  </c:choose>

</jsp:root>

Copy it and try it out.

0 comments

RSS feed

Tweet this article

geschiedenis

Dit is de zoveelste incarnatie van mijn website wie nieuwsgierig is kan de

oudere 'recente' wijzigingen bekijken.

En ik heb hier een pagina waarop ik nieuwe en oudere versies van my_editors verzamel.