MM/dd/yyyy format in p:calendar - jsf

In reference to this question,
it still accepts 8/1/201987 as a date, where in 201987 is not in yyyy format.
How to ensure yyyy format? Do I need to validate it in the bean?
I have used the code below :
<p:calendar ...>
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
But as it uses SimpleDateFormat, if you give yyyy it accepts any number of digits.
How to resolve this issue?

Try mask for p:calender like mask="99/99/9999", it's works fine for me
<p:calendar value="8/1/201989" mask="99/99/9999">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>

You can restrict the user input by adding attributes mask="true" and maxlength=10

Related

How to use today's date for a placeholder?

I got a <p:calendar> and want to use today's date as placeholder.
My attempt is to use OmniFaces #{now} in combination with a:placeholder for this:
<p:calendar id="calendar" a:placeholder="#{now}" minHour="8" locale="de"
maxHour="20" value="#{adminSeminarAlleBearbeitung.seminar.someproperty}"
pattern="dd.MM.yyyy HH:mm" />
Where someproperty is a java.util.date.
This is working so far, but I want to format the date like dd.MM.yyyy HH:mm.
How can I achieve this?
Use of:formatDate() function in EL:
<p:calendar ... a:placeholder="#{of:formatDate(now, 'dd.MM.yyyy HH:mm')}" />
Or if you'd like to reuse calendar component's own pattern attribute:
<p:calendar ... a:placeholder="#{of:formatDate(now, component.pattern)}" pattern="dd.MM.yyyy HH:mm" />
See also:
What exactly is #{component} in EL?

JSF - Formattting <p:calendar> using SimpleDateFormat

I have a <p:calendar> code in XHTML
<p:calendar
value="#{Bean.targetdate}"
pattern="MM/dd/yyyy" size="10" navigator="true"
rendered="#{Bean.editCmdActionflg == true}"
onblur="fChkDate(this);">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
My problem is when I provide a invalid date such as 12/31/6666 the <p:calendar> accept the date as it is and I did SOP in the backing bean and I got Mon Dec 31 00:00:00 IST 6666 for the date that I enter.
I should not allow the user to enter a invalid date like the one that I entered above.
And I need to make sure the user should provide a valid date How can I do that ?
You can use yearRange property to reslve your problem, like-
<p:calendar yearRange="c-125:c+50" value="#{Bean.targetdate}" pattern="MM/dd/yyyy" readonlyInput="true" size="10" navigator="true"
rendered="#{Bean.editCmdActionflg == true}"
onblur="fChkDate(this);"/>

regex date validation on p:calendar

my calendar has readOnlyInput="false" , hence the user can enter wrong dates such as 13/13/2013.
is there a way to regexValidate my date in case the user prefers to type the date instead of using the datePicker popup ?
<p:calendar id="birthDate" size="22" locale="#{view.locale}"
maxdate="#{userCreationBean.maxDate}" navigator="true"
yearRange="c-100" readOnlyInput="false"
value="#{userCreationBean.user.birthDate}"
mindate="01/01/1900" pattern="dd/MM/yyyy"
style="left: 194px !important;"
>
</p:calendar>
The <f:validateRegex> validator works on String input values only, not on Date input values and is therefore insuitable for the purpose you had in mind.
Rather use the <f:convertDateTime> converter.
<p:calendar ...>
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
It's by default non-lenient and will thus throw a converter exception when an invalid date is entered. You can if necessary customize the converter message by converterMessage attribute on the input component.

validating and displaying decimal numbers in JSF 2.0

I wonder how to validate an inputText text field and see if it matchs a decimal format.
And also in the rendering time how to format that text field with a specific format
I've done this :
<rich:column id="soldes_comptables">
<f:facet name="header">
<h:outputText value="Solde Comptable" />
</f:facet>
<h:inputText id="inputTextSC" value="#{file.soldes_comptables}"
label="Montant"
style="border-color:#F2F3F7;"
validatorMessage="The number must be decimal eg: 000.00"
>
<f:convertNumber pattern="#,###,##0.00"/>
<f:validateRegex pattern="^[0-9]+(\.[0-9]{1,2})?$"></f:validateRegex>
<rich:validator />
</h:inputText>
<rich:message for="inputTextSC"/>
</rich:column>
but it's not working as i want :(. please help me
You're mixing validation and conversion. The <f:validateRegex> applies only on String values, not on Number values, however, the <f:convertNumber> has already converted it from String to Number beforehand, so the <f:validateRegex> is rather useless to you. You should remove it and specify the message as converterMessage instead.
<h:inputText ... converterMessage="The number must be decimal eg: 000.00">
<f:convertNumber pattern="#,###,##0.00"/>
</h:inputText>
An alternative would be to create a custom converter extending NumberConverter and throw a ConverterException on improper input format based on some regex pattern matching.
See also:
validating decimals inputs in JSF
How validate number fields with validateRegex in a JSF-Page?

What is the cleanest way to display a java.util.Calendar object in JSF?

We have an object with java.util.Calendar objects. We would like to display the data on a JSF page (preferably in the same format we have for java.util.Date objects). It seems like there should be some clean way to do this other than creating a wrapper class just to convert the Calendar to a Date.
What is the cleanest way to display the date/time held in a java.util.Calendar in a JSF page?
Use Calendar's own getter Calendar#getTime(). It returns a Date. Then you can use <f:convertDateTime> the usual way.
<h:outputText value="#{bean.calendar.time}">
<f:convertDateTime type="both" dateStyle="long" />
</h:outputText>
For others and #seangates: You CAN apply a pattern if using the Calendar object. E.g.
<h:outputText value="#{bean.calendar.time}" styleClass="date">
<f:convertDateTime pattern="EEEE, MMMM d yyyy" />
</h:outputText>
<h:outputText value="#{bean.calendar.time}" styleClass="time">
<f:convertDateTime pattern="hh:mm" />
</h:outputText>
If you need futher Text:
<h:outputFormat
value="#This my date: {0,date,long}">
<f:param value="#{bean.calendar.time}" />
</h:outputFormat>
see: https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html

Resources