How to write an inputText that accepts only integers in the pattern #, ###. 00

Is there a way to write Text input that accepts only numbers, and also in template format #, ###. 00 to enter the currency number in JSF? (using PrimeFaces will be more appreciated)

Thanks...

+4
source share
3 answers

Mark the link

It says that you can use:

<p:inputMask value="# {maskController.date}" mask="99/99/9999"/> 

I have never used PrimeFaces before, but I used JSF. If you do not want to use javascript, you need to use the conversion tag inside the inputText tag.

 <h:inputText id="money" required="true"> <f:convertNumber maxFractionDigits="2" groupingUsed="true" currencySymbol="$" maxIntegerDigits="4" type="currency"/> </h:inputText> 

PD: RegEx is another option. RegEx means regular expression. This is a way to check if something like a string matches a rule. You can use in jsf with RegEx Validator.

+4
source

This code below works

 <script> <![CDATA[ function isNumber(event) { if (event) { var charCode = (event.which) ? event.which : event.keyCode; if (charCode < 48 || charCode > 57) return false; } return true; } ]]> </script> <p:inputText id="money" onkeydown="return isNumber(event);" /> 

to erase an available key

 <script> <![CDATA[ function isNumber(event) { if (event) { var charCode = (event.which) ? event.which : event.keyCode; if ((charCode < 48 || charCode > 57) && charCode!=8 && charCode!=46) return false; } return true; } ]]> </script> <p:inputText id="money" onkeydown="return isNumber(event);" /> 
+1
source

Adding an additional useful unlisted answer for this question:

You can simply use the extension tag mainfaces inputNumber : https://www.primefaces.org/showcase/ui/input/inputNumber.xhtml

+1
source

Source: https://habr.com/ru/post/1383900/


All Articles