Sat. Sep 18th, 2021

Requirement: I have an Email field for which I want to validate the entered Email. For this I want to write a Custom Validator inside the Managed Bean.

Solution: For solution of the above requirement follow the steps a shown below:

Step 1: Create an Oracle ADF Fusion Web Application. Create a demo.jspx page inside the ViewController project of the application. Let us have three af:inputText components for Name, Email, and Phone Number.

Step 2: Click on the Email field > Go to the Property Inspector > Go to the Validator property.

Create a Validator with the name as emailValidation inside the Managed Bean (MyBean)

Thus, the Validator property value will now look as shown below:

Also, set autoSubmit=”true” for the Email field.

Thus, the complete code for demo.jspx page is shown below:

And, the complete code for the MyBean.java is shown below.

package susantotech.com;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public class MyBean {

    public MyBean() {
    }

    public void emailValidation(FacesContext facesContext, UIComponent uIComponent, Object object) {
     if (object != null) {
         String name = object.toString();
         String expression = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
         CharSequence inputStr = name;
         Pattern pattern = Pattern.compile(expression);
         Matcher matcher = pattern.matcher(inputStr);
         String msg = "Please enter valid Email.";
         if (matcher.matches()) {
             //Do your further processing
         } else {
             throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null));
         }
      }
   }
}

Save all and run the application. Thus, the ran application is shown below:

Enter the value in the Email field as susantotech and move out of the Email field. Immediately, it will throw Email Validation error as shown below.

Hence, the solution to our requirement.

If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.

Thanks & Regards,
Susanto Paul

 404 total views,  1 views today

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

Leave a Reply

Your email address will not be published. Required fields are marked *