how to Validate double values with regular expression in jsf? - jsf

it should allow upto 30 digits before decimal and atleast 1 , atmost 2 digits after decimal

If you use JSF 2.0 you can use the regex-validator tag:
<h:inputSecret id="password" value="#{user.password}">
<f:validateRegex pattern="add your pattern here" />
</h:inputSecret>
If you are still on JSF 1.x I think you have to write your own custom validator by creating a class that implements javax.faces.validator.Validator interface. Here is a good tutorial how to achieve this.

Here in this example is the solution i suggest for validating double:
<h:inputText id="someField" value="#{yourBean.doubleValue}">
<f:convertNumber minFractionDigits="2" />
<f:validateDoubleRange minimum="1" maximum="30"/>
</h:inputText>
Use the attributes of the convertNumber tag to set the limits you need.

Related

JSF outputText and PrimeExtensions inputNumber [duplicate]

I am using JSF 2 and RichFaces 3. Here in the picture shown below, numbers are being displayed as what they are in the database.
But I want to display them as 6749395.20 if fraction part is there and 5095138.00 if no fraction part is there.
As of now I have tried something like this.
<rich:column>
<f:facet name="header">
<h:outputText value="Total Amount"/>
</f:facet>
<h:outputText value="#{rr[2]}">
<f:convertNumber type="number" groupingUsed="true" minFractionDigits="2" pattern="#0.00"/>
</h:outputText>
</rich:column>
Actually I am showing all of them together, but I have tried with all of them as all possible combinations with type, groupingUsed, minFractionDigits and pattern.
Why does it not work? How is this caused and how can I solve it?
That can happen if the value is not a Number at all, for example a String. You're then basically using the wrong type for the data it represents. To represent currencies in Java, you should be using BigDecimal. Also, make sure that the type in the database table is right, i.e. it should not be a varchar, but a decimal.
Once you've fixed the data type, then the <f:convertNumber> will work as you told it to do. Note that the pattern attribute will override the groupingUsed and minFractionDigits. You should use either the pattern or the others. Also, type="number" is already the default, so it can be removed.
So, either use
<f:convertNumber pattern="#0.00" />
or
<f:convertNumber groupingUsed="true" minFractionDigits="2" />
Note that they generate different formats. You probably want to set grouping to false.
You can also use type="currency", it will then automatically apply the right pattern as per the UIViewRoot#getLocale():
<f:convertNumber type="currency" />
See also the tag library documentation and the DecimalFormat javadoc.

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.

synchronize E mail automatically with database

I wanted to know from you whether it is possible to check an email entry, if it exists already in the database and to display a message .
This validation should work dynamically without a button.
I work with jsf 2.1 and prime faces 3.5
I hope you understand what I mean and thank you for your help.
You need to implement a custom validator. Full explanation in this guide
Snippet code:
<h:inputText id="email" value="#{bean.email}">
<f:validator validatorId="myEmailValidator" />
<f:ajax event="keyup" render="emailError" />
</h:inputText>
<h:message id="emailError" for="email" />

Format a JSF converted value

Today I noticed ocpsoft has a nice time library adapted to use in JSF 2 as a converter. The strong point of that is you can use the converter directly in the date displayed in the view and it converts it into a string telling the user something like 6 hours ago or 17 hours from now. I think my best is to combine both, the JSF converted date and this one to display something like 26-03-2013 17:00 (4 hours from now). I can achieve something similar with the following code:
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>
My problem comes when I want to put the second value into parenthesis. The PrettyTimeConverter accepts only a date as a value and I can't write the parenthesis there directly. Also JSF is not accepting the following:
<h:outputFormat value="({0})">
<f:param value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</f:param>
</h:outputFormat>
With that I have the following error:
<f:converter> Parent not an instance of ValueHolder:
javax.faces.component.UIParameter#1492636
Any idea about how to achieve that avoiding writing both parentheses using specific h:outputText tags?
You can just put those parentheses directly in template text without the need for another <h:outputText>s.
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
(<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>)
See also:
Is it suggested to use h:outputText for everything?

setting f:validatelength dynamically does not work

When I use a bean to set the min and max values on f:validatelength it simply does not work. It breaks the functionality in the form so it can't be submitted...
When I outputtext the values they do come out as legit numbers and hardcoding those numbers work fine.
My guess it that it somehow lacks the required connection to beans at that time in the lifecycle or something? (I'm pretty much a beginner). Example below is example of what I want to do
<h:inputSecret id="password" value="#{userBean.user.password}">
<f:validateLength minimum="#{myBean.minSize}" maximum="#{myBean.maxSize}" />
</h:inputSecret>
<h:message for="password" />
I tried using < a4j:keepAlive > on the bean but I couldn't get that to work.
I am using JSF 1.2 with Richfaces 3.3.3 and Seam 2.
Hopefully someone has a clue here. And a good way to achieve this using another tag then that's fine as well! Thanks for reading.
Is the el expression evaluated to integer type?
I have no idea, but you can try a couple of these variations
<h:inputSecret id="password" value="#{userBean.user.password}" maxlength="#{myBean.maxSize}">
<f:validateLength minimum="#{myBean.minSize}"/>
</h:inputSecret>
<h:message for="password" />
<h:inputSecret id="password" value="#{userBean.user.password}" maxlength="#{myBean.maxSize}">
<f:validator validatorId="javax.faces.validator.LengthValidator">
<f:attribute name="minimum" value="#{myBean.minSize}"/>
</f:validator>
</h:inputSecret>
<h:message for="password" />

Resources