Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PrimeFaces Beginner's Guide

You're reading from   PrimeFaces Beginner's Guide The perfect introduction to PrimeFaces, this tutorial will take you step by step through all the great features, ranging from form-creation to sophisticated navigation systems. All you need are some basic JSF and jQuery skills.

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781783280698
Length 378 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
K. Siva Prasad Reddy K. Siva Prasad Reddy
Author Profile Icon K. Siva Prasad Reddy
K. Siva Prasad Reddy
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Introduction to PrimeFaces FREE CHAPTER 2. Introducing Sample Application TechBuzz 3. Using PrimeFaces Common Utility Components 4. Introducing the PrimeFaces Client Side Validation Framework 5. Introducing Text Input Components 6. Working with Selection Input Components 7. Introducing Advanced Input Components 8. Working with Data Components 9. Introducing Advanced Data Visualization Components 10. Working with Layout Components 11. Introducing Navigation Components 12. Drawing Charts 13. Using PrimeFaces Themes Index

Time for action – validate e-mail using RemoteCommand

We will now take a look at how to invoke server-side logic from JavaScript code using RemoteCommand. First we will check the e-mail format on client side using JavaScript, and then invoke server-side logic to check if the user provided e-mail already in use or not. Perform the following steps:

  1. Create a method using following code in the UserController managed bean to check whether the given e-mail is already in use or not:
    @ManagedBean
    @RequestScoped
    public class UserController 
    {
      
      public void checkEmailExists()
      {
        String email = this.registrationUser.getEmail();
        if("admin@gmail.com".equals(email) || "test@gmail.com".equals(email))
        {
          String msg = "Email ["+email+"] already in use.";
          FacesContext.getCurrentInstance().addMessage("registrationForm:email", 
              new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
        }
      }
    }
  2. On the user registration page, create validateEmail() JavaScript function for checking e-mail format, and use a remoteCommand component to invoke the checkEmailExists() actionListener method to check whether the given e-mail is already in use or not:
    <!DOCTYPE html> 
    <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>
      <title>Home</title>
      <script>
      function validateEmail() 
      {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var email = $.trim($(PrimeFaces.escapeClientId("registrationForm:email")).val());
        if(email ==''){
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).text("");
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).attr("class", "");
          return;
        }
        if( emailReg.test( email ) ) {
          checkDuplicateEmail();       
        } else {
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).text("Invalid Email");
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).attr("class", "ui-message-error ui-widget ui-corner-all ui-message-error-detail");
         }
      }
      </script>
    </h:head> 
    <body> 
        
      <h:form id="registrationForm">
        <p:panel header="Registration Form" style="width: 800px;">    
          <h:panelGrid columns="3">
          
            <p:outputLabel value="Email:"/>
            <p:inputText id="email" value="#{userController.registrationUser.email}" onblur="validateEmail();" />
            <p:message id="emailMsg" for="email"/>
            
            <p:commandButton action="#{userController.register}" value="Register" update="registrationForm"/>  
            
          </h:panelGrid>
        </p:panel>
        <p:remoteCommand name="checkDuplicateEmail" actionListener="#{userController.checkEmailExists()}" update="emailMsg"/>    
      </h:form>    
    </body> 
    </html>

What just happened?

We have created a validateEmail() JavaScript function to check the e-mail format using Regex and will be called on onblur event on e-mail input element. In the validateEmail() function, if the e-mail format is invalid we are showing the error message as "Invalid email!!", otherwise we are invoking the remoteCommand checkDuplicateEmail as JavaScript function, which invokes the UserController.checkemailExists() method and add error message if the e-mail is already in use.

You have been reading a chapter from
PrimeFaces Beginner's Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781783280698
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime