Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:20:50+00:00 2026-05-28T08:20:50+00:00

For example: I have a JSF Validator to validate e-mail thusly: @FacesValidator(value=validateEmail) public class

  • 0

For example: I have a JSF Validator to validate e-mail thusly:

@FacesValidator(value="validateEmail")
public class Email implements Validator
{

private static final String EMAIL_REGEXP =
        "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

@Override
public void validate(FacesContext context, UIComponent c, Object val) throws ValidatorException
{
    String email = (String) val;
    Pattern mask = null;
    mask = Pattern.compile(EMAIL_REGEXP);
    Matcher matcher = mask.matcher(email);

    if (!matcher.matches()) {
        FacesMessage message = new FacesMessage();
        message.setDetail("Must be of the form xxx@yyy.zzz");
        message.setSummary("E-mail Addresss not valid");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
}
}

This validator will throw an exception if the user doesn’t enter an e-mail. However sometimes I want to make the e-mail an optional field. Is there a way to do this that doesn’t require me to write a different validator?

Ideally I would like it to check for a parameter somewhere in the JSF markup that uses it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:20:51+00:00Added an answer on May 28, 2026 at 8:20 am

    To answer your concrete question, you can make use of <f:attribute> which is enclosed in the same component.

    <h:inputText>
        <f:validator validatorId="validateEmail" />
        <f:attribute name="foo" value="bar" />
    </h:inputText>
    

    Inside the validate() method it’s available by UIComponent#getAttributes() of the 2nd UIComponent argument.

    String foo = component.getAttributes().get("foo"); // bar
    

    The better solution in your particular case, however, is just to let the validator return when the value is null or empty.

    if (email == null || email.isEmpty()) {
        return;
    }
    

    In JSF 1.x this would not be necessary because validators wouldn’t be called when the input is null or empty. But in JSF 2.x this has changed in order to support JSR303 bean validation. So if you want to skip validation on empty fields, you’d need to check this in every single JSF Validator.

    Note that when you have this in your web.xml

    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    

    then you can safely omit the email.isEmpty() part from the check because it would always be null instead of an empty string.


    Unrelated to the concrete problem, the pattern which you’ve there is outdated. These days unicode characters are allowed in email addresses.

    private static final String EMAIL_REGEXP = "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)";
    

    (note that ^ and $ are unnecessary because Pattern implicitly uses this already)

    Further I also suggest to do the Pattern#compile() directly on a static final variable. Compiling a pattern is relatively expensive and a Pattern instance is threadsafe anyway.

    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEXP);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my JSF 2.0 project i have 2 classes: public class driver { private
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public
I have implemented a JSF validator based on this example . How can I
I have a simple JSF 2.0 composite component example. <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
Why does: const char example; (uint64*)example have a value of 140734799798420 and *(uint64*)example have
Example: I have an class that inherits from UIImageView. An object creates an instance
I have the following JSF table: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have a problem with following tutorial: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/ In faces-context file you have to
Is there a way to make jsf validator handle empty strings? I have a
Consider the following jsf page: <h:body> <h:form id=SessionStartDialog> <table> <tr> <td class=label> <h:outputLabel value=Enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.