View Javadoc

1   /**
2    * 
3    */
4   package com.atlassian.xwork.validator.validators;
5   
6   import com.opensymphony.util.TextUtils;
7   import com.opensymphony.xwork.validator.ValidationException;
8   import com.opensymphony.xwork.validator.validators.FieldValidatorSupport;
9   
10  /**
11   * Validate that a field contains comma separated e-mail addresses.
12   * 
13   * @author Paul Curren
14   */
15  public class CommaSeparatedEmailValidator extends FieldValidatorSupport {
16  
17  	public void validate(Object object) throws ValidationException {
18          String fieldName = getFieldName();
19          String value = (String) getFieldValue(fieldName, object);
20  
21          if (value == null) {
22              return;
23          }
24  
25          value = value.trim();
26  
27          if (value.length() == 0) {
28              return;
29          }
30          
31          String[] emails = value.split("\\s*,\\s*"); // split on comma surrounded by option whitespace
32          
33          // now validate each e-mail address
34          for (int i=0; i < emails.length; i++) {
35          	if (!TextUtils.verifyEmail(emails[i])) {
36                  addFieldError(fieldName, object);
37                  break;
38              }        	
39          }
40  	}
41  }