Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Custom input validator

Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

<h:outputText 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");


Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic