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
Validating Mulitple Email Id Using Prime Faces
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:
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;
}
}
-
- Information
-
Who is online
Users browsing this forum: No registered users and 49 guests