File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Custom input validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Custom input validator" Watch "Custom input validator" New topic

Custom input validator

Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
Hi Guys,

i've got this code in my project to validate fields, i've made this my backend bean and added it to my faces.config.xml.when i call this bean to validate the fields, nothing happens. i call my bean like this, validation being the name of the class and validate surname is the method name in my input attribute, can anybody tell me what i'm doing wrong. i call individual methods for each and every field, to validate but no luck

<hutputText value="Surname" />
<h:inputText id="Surname" value="" required="true" validator="#{validation.validatesurname}" />
<h:message errorStyle="color: red"
infoStyle="color: green" layout="table" for="Surname" />

Back end bean

import javax.faces.validator.*;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;


public class Validation implements Validator {

public Date validateDate(FacesContext context, UIComponent component, String value) throws ConverterException, java.text.ParseException {

String pattern = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date nDate;

try {

nDate = sdf.parse(value);
} catch (ParseException ex) {
FacesMessage message = new FacesMessage();
message.setDetail("Date required (yyyy/MM/dd)");
message.setSummary("Date required (yyyy/MM/dd)");
throw new ConverterException(message);
if(nDate.getTime() > new Date().getTime()){
FacesMessage message = new FacesMessage();
message.setDetail("Invalid birth day to ID number");
message.setSummary("Invalid birth day to ID number");
throw new ConverterException(message);

return nDate;

public void validateEmail(FacesContext context,UIComponent toValidate, Object value) {
String email = (String) value;
if ( email.indexOf("@") == -1 || email.startsWith("@") || email.endsWith("@") ) {
throw new ValidatorException(new FacesMessage("Enter a valid email address."));

public void validatesurname(FacesContext context,UIComponent toValidate, Object value) {
String surname = (String) value;
if (surname=="") {
FacesMessage message = new FacesMessage("Capture surname");
context.addMessage(toValidate.getClientId(context), message);
public void validatePassport(FacesContext context, UIComponent toValidate, Object value) {
String passport = (String) value;
if ( passport=="" ) {
FacesMessage message = new FacesMessage("Invalid passport");
context.addMessage(toValidate.getClientId(context), message);

public void validateAddress(FacesContext context, UIComponent toValidate, Object value) {
String address = (String) value;
if ( address=="" ) {
FacesMessage message = new FacesMessage("Capture address");
context.addMessage(toValidate.getClientId(context), message);

public void CellNumberValidator(FacesContext context, UIComponent component, Object value) {
String strValue = (String)value;
if (strValue.length() != 10){
throwException("Number of cell phone digits must be 10");


Yasir Bajwa

Joined: May 02, 2007
Posts: 23
Not sure, but it doesn't look like the Validator interface is actually implemented. How is it compiling?

In your case with method calls I don't think you need to actually implement the Validator interface, but the validation methods should have methods with the same signature as the validate method in the Validate interface.

The validateDate method doesn't look quite right, I think java.text.ParseException is a checked exception, so it probably won't work as a validator method. I guess you could wrap any checked exception into a validator exception?
I agree. Here's the link:
subject: Custom input validator
It's not a secret anymore!