JSF - a regular expression validator

Continuing my example from a few days back, this example shows how to create a JSF validator tag which checks agains a regular expression.

Step 5: custom validator tag
  1. Add validator class

    package jsftest1;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.validator.Validator;
    import javax.faces.validator.ValidatorException;
    
    public class RegExValidator implements
            Validator {
    
        String regex;
    
        public void validate(
                FacesContext _context,
                UIComponent _component,
                Object _value)
                throws ValidatorException {
    
            String val = (String) _value;
            if (!val.matches(regex)) {
                throw new ValidatorException(
                        new FacesMessage(
                                "Incorrect value, does not match regular expression: "
                                        + regex));
            }
    
        }
    
        public String getRegex() {
            return regex;
        }
    
        public void setRegex(String regex) {
            this.regex = regex;
        }
    }
    
  2. Add validator tag class

    package jsftest1;
    
    import javax.faces.validator.Validator;
    import javax.faces.webapp.ValidatorTag;
    import javax.servlet.jsp.JspException;
    
    public class RegExValidatorTag extends
            ValidatorTag {
    
        private String regex;
    
        public RegExValidatorTag() {
            super();
            super.setValidatorId("regexValidator");
        }
    
        protected Validator createValidator()
                throws JspException {
            RegExValidator result = null;
            result = (RegExValidator) super.createValidator();
            result.setRegex(regex);
            return result;
        }
    
        public String getRegex() {
            return regex;
        }
    
        public void setRegex(String regex) {
            this.regex = regex;
        }
    }
    
  3. Add tag lib descriptor file /web/WEB-INF/jsftest1.tld

    <taglib>
      <tlibversion>0.1</tlibversion>
      <jspversion>2.0</jspversion>
      <shortname>JSF test 1 taglib</shortname>
      <tag>
        <name>regexValidator</name>
        <tagclass>jsftest1.RegExValidatorTag</tagclass>
        <attribute>
          <name>regex</name>
          <required>true</required>
          <rtexprvalue>false</rtexprvalue>
        </attribute>
      </tag>
    </taglib>
    
  4. Add validator to faces-config.xml

    <validator>
      <validator-id>regexValidator</validator-id>
      <validator-class>jsftest1.RegExValidator</validator-class>
      <attribute>
        <attribute-name>regex</attribute-name>
        <attribute-class>java.lang.String</attribute-class>
      </attribute>
    </validator>
    
  5. Modify jsp page

    <h:outputLabel for="firstName">
      <h:outputText value="#{bundle.firstName}"/>
    </h:outputLabel>
    <h:inputText id="firstName" value="#{registerBean.firstName}" >
      <f:validateLength minimum="1"/>
      <j:regexValidator regex="[A-Z]+"/>
      <f:validator validatorId="registerValidator"/>
    </h:inputText>
    <h:message for="firstName"/>
    
blog comments powered by Disqus