Wednesday, May 15, 2013

Simple number validation in JSF

There are many ready validator in JSF, eg. <f:validateLongRange />, <f:validateDoubleRange />, <f:validateRegex />, etc.
for more information about JSF validator, please refer here.

to validate number value, we can use the ready made JSF validator, that is <f:validateLongRange /> and <f:validateDoubleRange />

Sample code:
managedBean
private String longValue;
// getter & setter for longValue

private String doubleValue;
// getter & setter for doubleValue;

jsp or xhtml
<h:inputText value="#{bean.longValue}" id="longnumber"
    validatorMessage="invalid long number">
    <f:validateLongRange />
</h:inputText>
<p:message for="longnumber" id="message1"/>

<br />

<h:inputText value="#{bean.doubleValue}" id="doublenumber"
    validatorMessage="invalid double number">
    <f:validateDoubleRange />
</h:inputText> 
<p:message for="doublenumber" id="message2"/>

output:
validating long number and double number

Other input validation example in JSF:
  - Credit Card
  - Email
  - Number, with regular expression


Done!!


LinkWithin

Related Posts Plugin for WordPress, Blogger...