Sunday, May 19, 2013

How to use jQuery validation plugin in JSF

jQuery validation plugin is a lightweight, easy to use and powerful validation tool.
But when we want to use it with JSF, some special handling are required for it to work, due to the JSF component's clientId.

In this article, Primefaces 3.5 is used, and solution demonstrated below should be suitable for all other JSF implementations.

Steps:
1. Create the form and components that required validation.
<h:form id="myform">
  <p:inputText id="emailAddr" value="#{user.email}" />
</h:form>

2. If the target project is not JSF (strust, servlet, etc), the following syntax used.
<script>
$(document).ready(function(){
$("#myform").validate({
    rules: {
        emailAddr: {
            email: true
            }
        }
    });
});
</script>

because the component's clientId is generated by JSF, normally the clientId is in the following format - 'formId' + ':' + 'componentId'.
in this case, the clientId is going to be myform:emailAddr
By changing the clientId only is not enough, because the semicolon(:) in between the clientId may confuse jQuery.
Thus, we need to append single quote (') in front and behind of the clientId.
in this case, the clientId is going to be 'myform:emailAddr'


Solution:

<script>
$(document).ready(function(){
$("#myform").validate({
    rules: {
        'myform:emailAddr': {
            email: true
            }
        }
    });
});
</script>

Complete source for jQuery validation plugin in JSF:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js" />
 <script>
 $(document).ready(function(){
     $("#myform").validate({
         rules: {
      'myform:emailAddr': {
          email: true
          }
      }
  });
     });
 </script>
    </h:head>
<h:body> 
    <h:form id="myform">
        <p:inputText id="emailAddr" value="#{user.email}" />
    </h:form> 
</h:body>
</html>


Done!!


8 comments:

  1. Great post kian! I am having trouble using the equalTo:
    No matter how I format the ID such as 'formID:inputID', "formID:inputID", 'formID\\:inputID' it doesn't work. It keeps telling me it has to match the previous data even though it is the same, my only guess is it can't find the input ID.

    Any ideas?

    ReplyDelete
    Replies
    1. Hi John,
      After few attempts, I found this is working fine.
      equalTo: $( "#myform\\:password1" )

      Please let me know whether this is work for you too.

      Delete
    2. Thank you Kian! It works like a charm and you're a great help! :) Your site is a tremendous help btw, please keep it up!

      Delete
  2. Great post! Two days looking for this solution and then I found your post! Thank you! Lol

    ReplyDelete
  3. Hey Kian, Any ideas on how to implement validation if form is inside ui:repeat? JSF adds an extra numbered ID (ex, 0,1,2...etc...) So how would you target dynamically added forms? Ex: 'myform:0:emailAddr', 'myform:1:emailAddr

    ReplyDelete
    Replies
    1. a workaround came in my mind is to put the <script>...</script> into the ui:repeat
      and append the form id with the ui:repeat index.
      Do let me know the outcome as well.

      Thanks

      Delete
  4. Thanks Alot for this post you just saved me :) also need little help on my jsf page has submit button and when i press this button still form is getting submitted without any validation i dont know why this is calling my java method. please help me on this.

    Thanks,
    Vinod

    ReplyDelete
    Replies
    1. Hi Vinod,
      can you please post your button code in encoded format.
      http://www.opinionatedgeek.com/DotNet/Tools/HTMLEncode/Encode.aspx

      Delete

LinkWithin

Related Posts Plugin for WordPress, Blogger...