<f:convertNumber pattern="####.00"/> cuts off the numbers after comma PrimeFaces 3.5/JSF 2.0 - jsf

I am using PrimeFaces 3.5 with JSF 2.0. I have fields, which is used to enter an amount in the format like 100000.00. sometimes users enter the amounts including commas like 15,234.00 and when they submit the page, it cuts off the numbers after comma and pass only 15. Kindly suggest?
I am using the below converter and validator to validate the field.
<p:inputText value="#{myBean.EstLossAmt }" converterMessage="Please enter only numeric values" id="estimatedLoss">
<f:convertNumber pattern="####.00"/>
<f:validateDoubleRange minimum="0.00" maximum="999999999.99"/>
</p:inputText>

Related

Force to use comma as a decimal separator in inputText field

I'm having a inputText where a number having a comma as a decimal separator is expected. The backend amount field is a BigDecimal, so the actual number going to the backend should be in the form 100.00 while I want the UI to show 100,00 and not accept 100.00.
I have looked at the inputNumber component which kinda does what I want, but we do not have that dependency in our project, so it's out of discussion.
I have also tried adding <f:convertNumber pattern="#0,00" />, but that does not help or I don't know how to use it...
Any help would be appreciated.
<p:inputText id="grossPayment"
styleClass="greyText boldText"
converterMessage="#{nts['grossPaymentNumbers']}"
requiredMessage="#{nts['grossPaymentRequired']}"
required="#{bean.profile.salary.required}"
value="#{bean.profile.salary.amount}">
<f:validator
disabled="#{!bean.profile.salary.required}"
validatorId="positiveNumberValidator" />
</p:inputText>

how to restrict only letters and numbers at p:inputMask

I want to restrict my <p:inputMask> with only letters and numbers without giving any size (max-legth) limit. I tried to use mask attribute but when I use mask attriibute it gives a max-length for my inputMask. Could you please help me to solve this issue ?
Sample Code :
<p:inputMask value="#{gercekKrediBasvuruDetayGirisView.kisi.ticariHarfSeri}"
required="true" mask="***"
requiredMessage="#{msg['GercekKrediBasvuruTuketiciMaliBilgiler.belgeSeriNull']}"
/>
If your input does not have to be <p:inputMask> and can be <h:inputText> or <p:inputText> then the easiest way to go would be to use it with regex:
<h:inputText id="inputField" value="#{backingbean.username}" validatorMessage="Value does not match pattern.">
<f:validateRegex pattern="^[a-zA-Z0-9]+$" />
</h:inputText>
<h:message for="inputField" />
If you only allow lower case letters go with ^[a-z0-9]+$.
<p:inputMask> is designed for somewhat stricter validation than only letters and numbers.

JSF 1.2/Seam 2.2 - validator is skipped when input it empty

I am trying to call validateLength in JSF (xhtml file of seam)
<f:validateLength minimum="2" maximum="512"/>
It works for values such as text length 513 and 1 (i.e. it shows a warning, but not for 0)
The input text filed for which it is being used is set to required=false (so that it can use a4j support for empty fields, I have to show a preview based on the input)
The problem that I see is that there a validator method in a helper class but it gets ignored when the length of input is 0 (ie I put nothing , it works for non-empty values).
I also have a NullableStringConverter here but what I have noticed that as soon as that converter sets the value of null of empty string, the validator gets skipped. Here is the complete snippet of in the inputText
<h:inputText id="linkNameInput"
value="#{someHelper.name}"
validator="#{someHelper.validateMethod}"
required="false">
<f:validateLength minimum="2" maximum="512"/>
<f:converter converterId="NullableStringConverter" />
</h:inputText>
I would just like the ability to validate an empty string in a validator.
I would just like the ability to validate an empty string in a validator.
That's not possible in JSF 1.x. That's only possible since JSF 2.0 (in favour of JSR303 Bean Validation support).
I'm not sure why you'd like to validate the empty string in the custom validator. There you normally use the required="true" attribute for this. If the sole goal is to change the required message, then just use requiredMessage attribute.
<h:inputText ... required="true" requiredMessage="Please enter name" />
As mentioned already, validating an empty input String in a JSF 1.2 component is not supported. However, you can check for this null/empty model value in your backing bean on form submit and add a FacesMessage that is mapped to that specific input field. This would allow integration with server-side component-specific error message display (<h:message for="componentId" />).
FacesContext.getCurrentInstance().addMessage("componentId",
facesMessageForComponent);

Validation problem for ensuring that entered value contains alphabet only

How can i make sure at conversion and validation phase during the JSF lifecycle that entered value in <h:inputText> contains alphabets only?
Thanks in advance.
You can use <f:validateRegex> for this.
<h:inputText id="input" value="#{bean.input}" validatorMessage="Please enter alphabets only">
<f:validateRegex pattern="[a-zA-Z]*" />
</h:inputText>
It accepts the same regex syntax as the Pattern class. Check its documentation. You can also use \p{Alpha} instead.
<f:validateRegex pattern="\\p{Alpha}*" />
Or if you're using bean validation (as confirmed by your question history), then you can also use #Pattern for this.
#Pattern(regexp="\\p{Alpha}*", message="Please enter alphabets only")
private String input;

Formatting a double in JSF

I have a problem similar to the one found here : JSF selectItem label formatting.
What I want to do is to accept a double as a value for my and display it with two decimals. Can this be done in an easy way?
I've tried using but that seems to be applied on the value from the inputText that is sent to the server and not on the initial value in the input field.
My code so far:
<h:inputText id="december" value="#{budgetMB.december}" onchange="setDirty()" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
</h:inputText>
EDIT: The above code actually works. I was fooled by JDeveloper that didn't update the jsp page even when I did a explicit rebuild of my project and restarted the embedded OC4J server. However, after a reboot of my computer everything was fine.
If I'm not misunderstanding your requirement, I was able to achieve formatting of the value in the input box during the rendering of the view with:
<h:inputText id="text1" value="#{...}">
<f:convertNumber pattern="#,###,##0.00"/>
</h:inputText>
I was using the Standard Faces Components in my vendor-branded Eclipse so I'm assuming the pattern attribute is part of standard JSF.
If what you are trying to do is make the value of the input text field change on screen (to correct user input), you should probably look into using one of the JSF ajax frameworks like Rich Faces.
A possible example would look like this:
<h:inputText id="december" value="#{budgetMB.december}" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
<a4j:support event="onblur" reRender="december" />
</h:inputText>
I haven't tested this, but I think it may work.
It seems you're actually formatting a currency. There already exists a specific formatter to handle currencies that you can assign many options to:
<f:convertNumber type="currency" />
Some interesting attributes of this tag are: locale, currencyCode, integerOnly, currencySymbol and pattern.

Resources