Validating an Email Address in JSF

In earlier installments of the jAstrologer tutorial (see previous blogs) we used a converter and standard validators to validate the input in inputText components. You can also code your own validators if the standard JSF validatiors do not work for you. In our example, we will code a validator that checks a string to see if it is a valid email. To create a custom validator, you create a class that implements the javax.faces.validator.Validator interface and register the class in faces-config.xml. You can then use the validator using the <f:validator> tag.

  1. Right-click the project node and choose New > Java Class. Name the class EmailValidator, place it in the astrologer.validator package, and click Finish.
  2. In the class declaration, implement Validator as follows:
    public class EmailValidator implements Validator {
  3. Use the hint to implement the validate method.

  4. Add the following code to the validate method:
        public void validate(FacesContext facesContext, 
                UIComponent uIComponent, Object object) throws ValidatorException {
            
            String enteredEmail = (String)object;
            //Set the email pattern string
            Pattern p = Pattern.compile(".+@.+\\\\.[a-z]+");
            
            //Match the given string with the pattern
            Matcher m = p.matcher(enteredEmail);
            
            //Check whether match is found
            boolean matchFound = m.matches();
            
            if (!matchFound) {
                FacesMessage message = new FacesMessage();
                message.setDetail("Email not valid");
                message.setSummary("Email not valid");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }
        }
    
  5. Open faces-config.xml and add the following code:
         ...
        </application>
        <validator>
            <validator-id>astrologer.EmailValidator</validator-id>
            <validator-class>astrologer.validate.EmailValidator</validator-class>
        </validator>
    </faces-config>
  6. Open greeting.jsp and add the email field:
         ...
            <p>Enter your name: <h:inputText value="#{UserBean.name}" 
            id="name" required="true"/>
            <h:message for="name" /></p>
            <p>Enter your email: <h:inputText value="email" 
            id="email" required="true">
                <f:validator validatorId="astrologer.EmailValidator" />
            </h:inputText>
            <h:message for="email" /></p>
            <p>Enter your birthday: <h:inputText value="#{UserBean.birthday}" 
                id="birthday" required="true">
         ...
  7. Run the project. When you enter a non-valid email in the field, you get the following error:

Comments:

Just wanted to say that your tutorials are very easy to understnad and learn. I appreciate what you are doing. It is helping me and I am sure it is helping a lot of other people come to terms with JSF. I am a poor student and cant afford to buy books so these kind of tutorials definitely help a lot. Thanks once again and I look forward to reading about the datbase query section whenver you get time to write that one up.

Posted by Surya on May 26, 2006 at 06:13 PM CEST #

I'm really happy to hear that the lessons are helping. I've often wondered if they are too basic or not. Definitely looking towards helping JSF newbies here, so glad you're finding them useful.

Posted by guest on May 30, 2006 at 03:46 AM CEST #

Post a Comment:
Comments are closed for this entry.
About

johnc

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today