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 4611832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:19:17+00:00 2026-05-22T01:19:17+00:00

In my jsf application I know how to validate user input against one pattern

  • 0

In my jsf application I know how to validate user input against one pattern in my ice:selectInputDate using a jsf converter:

   <f:convertDateTime pattern="MM/dd/yyyy"  />

but how should I do if I want to let the user be able to enter a date in format: “MM-dd-yyyy” too?

I think this can be done extending DateConverter from jsf but I already tried with that and I fail. Do you have a valid example to validate input date against multiple patterns?

Thanks.

UPDATE: I’m using jsf 1.2

  • 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-22T01:19:18+00:00Added an answer on May 22, 2026 at 1:19 am

    Create a custom converter which accepts multiple patterns by <f:attribute> on the component.

    Here’s how you’d like to have the view to look like:

    <h:inputText id="input" value="#{bean.date}">
        <f:converter converterId="multiDateConverter" />
        <f:attribute name="pattern1" value="MM/dd/yyyy" />
        <f:attribute name="pattern2" value="MM-dd-yyyy" />
    </h:inputText>
    

    And here’s how the converter can look like (for JSF 1.x, register it as

    <converter-id>multiDateConverter</converter-id>
    

    in faces-config.xml instead)

    @FacesConverter(value="multiDateConverter")
    public class MultiDateConverter implements Converter {
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
            List<String> patterns = getPatterns(component);
            Date date = null;
    
            for (String pattern : patterns) {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                sdf.setLenient(false); // Don't parse dates like 33-33-3333.
    
                try {
                    date = sdf.parse(value);
                    break;
                } catch (ParseException ignore) {
                    //
                }
            }
    
            if (date == null) {
                throw new ConverterException(new FacesMessage("Invalid date format, must match either of " + patterns));
            }
    
            return date;
        }
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
            return new SimpleDateFormat(getPatterns(component).get(0)).format((Date) value);
        }
    
        private static List<String> getPatterns(UIComponent component) {
            List<String> patterns = new ArrayList<String>();
    
            for (int i = 1; i < Integer.MAX_VALUE; i++) {
                String pattern = (String) component.getAttributes().get("pattern" + i);
    
                if (pattern != null) {
                    patterns.add(pattern);
                } else {
                    break;
                }
            }
    
            if (patterns.isEmpty()) {
                throw new IllegalArgumentException("Please provide <f:attribute name=\"patternX\"> where X is the order number");
            }
    
            return patterns;
        }
    
    }
    

    Note that it only picks the first (default) pattern to redisplay the value. So in the above example, if you enter 05-10-2011, then it get redisplayed as 05/10/2011.


    Unrelated to the concrete problem, the pattern MM-dd-yyyy isn’t very common. Didn’t you mean to use dd-MM-yyyy?

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

Sidebar

Related Questions

I am using jsf reach faces in one application. I am using rich:modalPanel for
I try to run jsf application in myeclipse using jboss web server and following
In JSF application we are using richfaces (Final.3.3.3). In pom.xml there is dependency on
I'm developing a JEE6 JSF application using NetBeans and its RAD development features. I
In a certain page of our JSF application, the user sees a table listing
I've a tomcat (with JSF) application. I want to know the path of the
My JSF application redirects any user who is not logged in to the login
In my JSF application, I need to redirect the user from Page A to
I'm developing a web application using Java EE6 and JSF 2.0. I have a
I wanted to know ways of checking my jsf application in terms of functioning

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.