Validating Mulitple Email Id Using Prime Faces

UI Components for JSF
Post Reply
bhanwar
Posts: 1
Joined: 14 Jan 2011, 17:15

14 Jan 2011, 17:18

Hi,

I need to validate comma separated multiple email addresses in a single field, and finding it difficult to do using PrimeFaces AJAX interface.

Please Suggest !!

Bhanwar Gupta
http://www.bhanwar.com

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

14 Jan 2011, 17:48

is it a simple text-input for which you want to validate the email-adresses?

if so, you can write your own JSF-validator and attach it to your input-field:

Code: Select all

public class CommaSeparatedEmailValidator implements Validator {
	@Override
	public void validate(FacesContext facesContext, UIComponent uiComponent,
			Object object) throws ValidatorException {
		String input = (String) object;
		StringBuilder invalidEmailAddresses = new StringBuilder();
		
		String[] emailAddresses = input.split(",");
		for (String emailAddress : emailAddresses) {
			if (!isValid(emailAddress)) {
				invalidEmailAddresses.append("'" + emailAddress + "',");
			}
		}
		
		if (invalidEmailAddresses.length() > 0) {
			FacesMessage message = new FacesMessage();
			String msg = "The following E-Mail Addresses were not valid: " + invalidEmailAddresses.toString() + " change them accordingly in order to continue";
			message.setDetail(msg);
			message.setSummary("Validation Error");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
		
	}
	
	private boolean isValid(String emailAddress) {
		// Set the email pattern string
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

		// Match the given string with the pattern
		Matcher m = p.matcher(emailAddress);

		// Check whether match is found
		boolean matchFound = m.matches();

		if (!matchFound) {
			return false;
		}
		return true;
	}
}

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 49 guests