views:

34

answers:

0

I have validator classes in my application that display error messages in the ice:messages control but due to design issues I want to change the validation to only let the user insert the correct input. For Example if the user presses numbers in a text field that should take the name, it just won't let him type numbers.

How can I possibly do that ?? should I change my validator class or should I write some Javascript code??

Here is my mobile number validator class for example:

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.Validator;
import javax.faces.validator.ValidatorException;
import univportal.util.MessageResource;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author admin
 */
public class MobileNumValidator implements Validator {

    private static final String MOBILE_NUM = "[0-9]{3}-? ?[0-9]{3}-? ?[0-9]{4}";
    private static final String MOBILE_NUM2 = "[0-9]{3,4}-? ?[0-9]{6,7}";

    public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException {
        //creating the mask
        Pattern pattern1 = Pattern.compile(MOBILE_NUM);
        Pattern pattern2 = Pattern.compile(MOBILE_NUM2);

        //retrieveing the data from the text field
        String mobilenumber = (String)arg2;

        //checking!
        Matcher m1 = pattern1.matcher(mobilenumber);
        Matcher m2 = pattern2.matcher(mobilenumber);

        if(!m1.matches() && !m2.matches()){
             FacesMessage fm = new FacesMessage();
             String error = "InvalidMobileNum";
             fm.setDetail(MessageResource.getMessage(error));
             fm.setSummary(MessageResource.getMessage(error));
             fm.setSeverity(FacesMessage.SEVERITY_ERROR);
             throw new ValidatorException(fm);
        }


    }

}

Another option is that I keep the error messages, but does anyone know a good tutorial website for working with ice:messages in icefaces design to make the error appear only under the field or show the error messages nicely without affecting the surrounding panels??

Thanks,