Diyae
Diyae

Reputation: 21

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

Upvotes: 0

Views: 10508

Answers (1)

BalusC
BalusC

Reputation: 1109705

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:

Upvotes: 5

Related Questions