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
527 total views, 1 views today