0

I have a input field which takes double can have only one of this pattern:

pattern="#,###,##0.00"

I've done this :

<h:inputText id="value"
             required="true"
             converterMessage="#{msg.invalidValue}"
             requiredMessage="#{msg.required}"
             value="#{technicalBean.value}">

    <f:convertNumber pattern="#,###,##0.00" />
</h:inputText>

<h:message  for="value"/>

But it's not working as I want. It accepts character, if the first input is number like as 8A and converts number only.

Tiny
  • 27,221
  • 105
  • 339
  • 599
ashour
  • 11
  • 1

1 Answers1

0

If you want to limit user to only type digits and . you can create javascript function and invoke it in onkeypress attribute or do this inline like this

<h:inputText id="value" required="true"
                 onkeypress="if ((event.which == 8) || (event.which == 46)) return true; if (event.which &lt; 48 || event.which &gt; 57) return false;"
                 converterMessage="#{msg.invalidValue}"
                 requiredMessage="#{msg.required}"
                 value="#{technicalBean.value}">
    <f:convertNumber  pattern="#,###,##0.00" />
</h:inputText>
<h:message  for="value" />

This will allow only digits, . and backspace (numbers are ASCII values for buttons).

If you are open for new framework, PrimeFaces got <p:inputMask> component which you can use for this purpose.

Geinmachi
  • 1,251
  • 1
  • 8
  • 20
  • I don't have problem to limit user but on key press not give me enough validation because user can copy and paste wrong data, thank you for answer. – ashour Sep 13 '15 at 13:25
  • about mask is very limited to user. – ashour Sep 13 '15 at 13:29
  • What you did is not validation but converter, if you want validation you should make validator or use JSR303 Bean Validation like `@Pattern` and after user presses key validate input. – Geinmachi Sep 13 '15 at 13:33
  • i want to use pattern for view old data not for validation ,only i want to validate number. – ashour Sep 13 '15 at 13:49
  • I believe you have the same problem like [here](http://stackoverflow.com/questions/11253368/jsf-convertnumber-tag-issue-with-trailing-alphabatic-char) if you don't want to allow `double` value with alphabetic characters after digits. – Geinmachi Sep 13 '15 at 14:37
  • thanks for you, this answer enhancement my logic and enough to me :) – ashour Sep 13 '15 at 15:03