How to use f:validateRegex on a numeric value - jsf

I used this regex ^(\+968|968|00968)?\d{8}$ for a phone number. It must be 8 digits and start with 9 or 7.
<p:inputText value="#{customer.phoneNo}" maxlength="8" required="true"
validatorMessage="Phone Number Should be 8 digits and start with 9,7">
<f:validateRegex pattern="^(\+968|968|00968)?\d{8}$"/>
<f:validateLength minimum="8" maximum="8"/>
<p:keyFilter mask="num"/>
</p:inputText>
but I'm getting this error:
java.lang.Integer cannot be cast to java.lang.String
In the bean phone number is an Integer.

f:validateRegex expects a String value (or CharSequence depending on the JSF implementation), so it cannot be used on an Integer. So you need to use a String value in your UI and convert that to numeric before saving it to the database. You could use an AttributeConverter for that if you are using JPA. But I must say that storing a phone number as a numeric database field is a strange choice in my opinion.
See also:
https://github.com/eclipse-ee4j/mojarra/blob/master/impl/src/main/java/jakarta/faces/validator/RegexValidator.java
https://github.com/apache/myfaces/blob/master/api/src/main/java/jakarta/faces/validator/RegexValidator.java

It means java.lang.Integer cannot be java.lang.String you need to convert it.

Related

p:inputText decimal separator

i want a comma decimal separator when user press decimal key, but after many test, not work any.
number is String type why on db is definited string
tried
<p:inputText value="#{testController.number}">
<f:convertNumber locale="it"/>
</p:inputText>
tried
<default-locale>it_IT</default-locale>
in faces-config
but any success.
Suggest?
If you use <p:inputNumber> it automatically generates commas for your integers.

f:convertNumber with input jsf

I have this piece of code:
<p:inputText value="#{addnewfields.price}">
<f:convertNumber type="number" />
</p:inputText>
So what I want to do is to display my number with space separators.
But still in my input it looks like 1000000 instead of 1 000 000.
Any ideas?
A possible solution you can try is to create your own converter. The InputText value is a String, you have to manage the conversion from this String to the object number you want.
The converter has two functions to implement: from the object (binded value) to String (text shown in the input)
and from the String to the object.
You can see a converter example here: http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/

Applying locale to <h:inputText> for double value

How to apply locale to <h:inputText> for decimal values in Seam Application(JSF-richfaces)? If I add an <f:convertNumber/> to my <h:inputText> the correct decimal delimiter is shown (e.g. 6,28 instead of 6.20). But when I enter the input value & save, following error message is displayed, if converter="javax.faces.Double" attribute used in <h:inputText>, if not, an exception is thrown.
Scenario: If locale is to set to German(de), try to enter the input value in en-US format or vice-versa.
Error Message/Exception:
Can't set property 'xxx' on class 'xxx' to value '590'.:
java.lang.IllegalArgumentException:
java.lang.ClassCastException#11e51bd
Also let me know, if any alternate solution is available.
Below is the code snippet :
<h:inputText id="iEuroDollar" value="#{tempCurr.currencyRateEuroDollarValue}" rendered="#{tempCurr.editRates}" maxlength="6" converter="javax.faces.Double" converterMessage="#{msg['error.double.field']}" >
<f:convertNumber minFractionDigits="2" locale="#{rolePoolAction.requestLocale}"/>
<a4j:support event="onblur" action="#{conversionAction.checkInputValue(tempCurr)}" reRender="iDollarEuro"/>
</h:inputText>
Thanks in Advance....

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
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:
validating decimals inputs in JSF
How validate number fields with validateRegex in a JSF-Page?

JSF Number Validation

Is there any inbuilt number validator tag in JSF that checks whether an input entered in h:inputext field is a number?
The first question was answered. Edited to explain the next problem:
<h:inputText id="maxrecs" value="#{simpleBean.numRecords}" required="false" maxlength="4" >
<f:convertNumber longOnly="true"/>
</h:inputText>
Backing Bean
private long numRecords = null;
If I use String or Integer object in the backing bean , value is not being set. Now when I use primitive int, 0 is being printed on the screen. I would like the screen to be empty.
You can use f:convertNumber (use the integerOnly attribute).
You can get more information here.
You can use:
<f:validateLongRange minimum="20" maximum="1000" />
Where minimum is the smallest number allowed and maximum is the largest.
Look here for more details
JSF Number validation for inputtext
mention f:converterNumber component in between h inputText component and mention the attributes integerOnly and type.
<h:inputText id="textMobileId" label="Mobile" styleClass="controlfont" value="#{UserRegistrationBean.textMobile}">
<f:convertNumber integerOnly="true" type="number" />
</h:inputText>
If you enter abcd in Mobile textbox at the time when you click on commandbutton it automatically shows an error like
Mobile: 'abcd' is not a number.
i8taken solution converts number into long without validation message (at least in my case: JSF2 / global messages per page). For proper validation message you can
1. check value in action method in bean;
or
2. use converter attribute for inputText:
<h:inputText id="maxrecs" value="#{simpleBean.numRecords}" maxlength="4" converter="javax.faces.Integer" />
You can simply use the passthrough, so first add this library
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
and after use this
<h:inputText id="numberId" pt:type="number" />

Resources