The Artima Developer Community
Sponsored Link

Java Buzz Forum
JSF: a regular expression validator

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Andrej Koelewijn

Posts: 594
Nickname: andrejk
Registered: Nov, 2002

Andrej Koelewijn is a Java and Oracle consultant
JSF: a regular expression validator Posted: Mar 25, 2004 2:45 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Andrej Koelewijn.
Original Post: JSF: a regular expression validator
Feed Title: Andrej Koelewijn
Feed URL: http://feeds.feedburner.com/AndrejKoelewijn
Feed Description: On Oracle, Java and OpenSource
Latest Java Buzz Posts
Latest Java Buzz Posts by Andrej Koelewijn
Latest Posts From Andrej Koelewijn

Advertisement
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 <PROJECT_HOME>/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"/>

Read: JSF: a regular expression validator

Topic: Interesting post from Dave Platt peeing on UDDI Previous Topic   Next Topic Topic: Groovy JSR

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use