APIs for the Internet of Things

Max Katz

Subscribe to Max Katz: eMailAlertsEmail Alerts
Get Max Katz: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: RIA Developer's Journal, Open Source and Cloud Computing, Open Source AJAX

Blog Feed Post

JSF Mojarra Extension Tags Validation and Focus

JSF Mojarra comes with a small extension tag library with three tags

Here is something you may or may not know. JSF Mojarra comes with a small extension tag library with three tags: regular expression validator, credit card validator, and focus setting tag. They are available since JSF version 1.2_09, but maybe even earlier (I didn’t check).

Page setup
Before using any of the tags, add this xml namespace to your Facelet page:


Regular expression validator
mj:regexpValidator is probably all you need to validate any kind of input by setting the right expression. For example, to validate an email address:

<h:panelGrid columns="3">
   <h:outputText value="Email:" />
   <h:inputText id="email" value="#{bean.email}"
          validatorMessage="Invalid email">
       <a4j:support event="onblur" />
       <mj:regexValidator pattern="[email protected]+\.[a-z]+" />
   <rich:message for="email" />

Adding RichFaces into the mix to invoke validation onblur event.

Credit card validator
mj:creditcardValidator uses Luhn Algorithm to validate the input.

<h:panelGrid columns="2">
<h:outputText value="Credit card:" />
   <h:inputText id="cc" value="#{bean.creditCard}">
	<a4j:support event="onblur" />
	<mj:creditcardValidator />
<rich:message for="cc" />

Lastly, mj:focus tags lets you set a focus on any component inside the current form:

<h:form id="form">
   <h:inputText id="address" value="#{bean.address}"/>
<mj:focus for="form:address"/>

The only thing to remember is to set for attribute to client id, not the component id.

That’s it, enjoy these “secret” tags.

Read the original blog entry...

More Stories By Max Katz

Max Katz heads Developer Relations for Appery.io, a cloud-based mobile app platform. He loves trying out new and cool REST APIs in mobile apps. Max is the author of two books “Practical RichFaces” (Apress 2008, 2011), DZone MVB (Most Valuable Blogger), and is a frequent speaker at developer conferences. You can find out what Max is up to on his blog: http://maxkatz.org and Twitter: @maxkatz.