Primefaces Input for Money Input (exactly 2 digits after floating point)

13,631

A regular expression was the best way I've found so far

<p:inputText id="numInput" value="#{val.value}" required="true"
    label="#{val.title}" validatorMessage="Not valid Number">
    <p:ajax event="change" process="@form" update=":edit_main" />
    <f:validateRegex pattern="^[-+]?[0-9]*\.?[0-9]{1,2}+$" />
</p:inputText>
<p:message for="numInput" />
Share:
13,631
yannicuLar
Author by

yannicuLar

Bad code leads to anger. Anger leads to hate. Hate leads to suffering.

Updated on July 24, 2022

Comments

  • yannicuLar
    yannicuLar almost 2 years

    I need a PrimeFaces input component, to get/set an amount of cash, that means a Decimal with 2 digits after the floating point.

    I tried using inputMask, like

    <p:inputMask value="#{moneyBean.amount}" mask="999.99"/>
    

    But I can't find some way to set a mask that accepts:

    1. 1 or more arithmetic values before floating point
    2. Optionally, a floating point "."
    3. 0 to 2 arithmetic values after the floating point

    For Example, some valid inputs would be:

    1. 1234.56
    2. 1234.5
    3. 2.8
    4. 120
    5. 120.00

    Any ideas to get this input in an efficient way?