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

  • SEARCH
  • Home
  • 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 6169795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:55:05+00:00 2026-05-23T22:55:05+00:00

I am using JSF2.0/primefaces I want to create a validator, the validator should validate

  • 0

I am using JSF2.0/primefaces

I want to create a validator, the validator should validate the format of an Input text (the Input has the type Integer).The problem is that the validator always displays Not valid.

This is the validator:

public void validateInputHours(FacesContext context, UIComponent component,
    Object value) throws ValidatorException {
Pattern p = Pattern.compile("[0-24]");
Matcher m = p.matcher((String) value);
if (!m.matches())
    throw new ValidatorException(new FacesMessage(
            FacesMessage.SEVERITY_ERROR, "Not valid",
            "Not valid"));
}

The input Text:

<p:inputText  validator="#{Control.validateInputHours}" style="width: 18px"/>
  • 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-23T22:55:06+00:00Added an answer on May 23, 2026 at 10:55 pm

    You state:

    the validator should validate the format of an Input text (the Input has the type Integer)

    and then your validator performs:

    Matcher m = p.matcher((String) value);

    which should result in a ClassCastException being thrown if you attempt to cast an Integer to a String. I suspect you are “eating” the ClassCastException somewhere, and you are issuing a generic message with value Not valid.

    The following snippets, for instance, work (against Primefaces 3.0M2):


    The Facelet:

    <h:form>
        <p:inputText value="#{control.value}"  validator="#{control.validateInputHours}" style="width: 18px"/>
        <h:commandButton action="#{control.process}" />
        <h:messages globalOnly="true" />
    </h:form>
    

    The Managed Bean:

    private int value;
    
    public int getValue() {
        return value;
    }
    
    public void setValue(int value) {
        this.value = value;
    }
    
    ...
    public void validateInputHours(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
        Integer hours = (Integer) value; // the value will be the value from the model after the Apply-Request-Values phase is complete
        if (hours < 0 || hours > 24) {
            throw new ValidatorException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Not valid",
                    "Not valid"));
        }
    }
    

    If you want to check the format of the value against a regex, then it’s representation in the model should be a String, and not an Integer/int. There again, Jon Skeet’s answer is valid. [0-24] will not do what you think it is doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically create controls in my bean. I am using JSF 2.0
I am using Primefaces and JSF2 . All my .xhtml files are dumped in
Simple question (using JSF 2.0 and primefaces 2.2.1): I need to create a button
We are using Primefaces 3M4 and one of our pages has a p:dataTable which
I have using jsf2.0 in eclipse ide.I have a problem to store a image
i'm making a JSF2.0 project using mojarra primefaces tomcat6.x. I made a select list
Has anybody used JSF2, PrimeFaces, and Highcharts together? I am really confused on how
I am running an application using JSF2.0 and Primefaces 2.2RC2 I have run the
I'm using JSF2 and GlassFish, PrimeFaces 2.1. This works, showCreateProfile() method gets hit, and
I have a problem using JSF 2.0, PrimeFaces and the tuckey.org UrlRewriteFilter. The problem

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.