Validation of multiple email addresses in flex

For our flex application, I had a ‘cc’ field that could handle multiple email addresses. When adding validation, I first used the standard EmailValidator class. However, this would not work as soon as you want to add multiple email addresss, like:
foo@company.com;bar@company.com

Luckily, creating a custom validator to handle this case is quite easy:

	import mx.validators.EmailValidator;

	import org.as3commons.lang.StringUtils;

	/**
	 * This validator allows validation of multiple email addresses
	 * that are separated by a semicolon (;)
	 */
	public class CompositeEmailValidator extends EmailValidator
	{
		public function CompositeEmailValidator()
		{
		}

		override protected function doValidation( value:Object ):Array
		{
			var result:Array = [];

			var emailsString:String = String( value );
			var emails:Array = StringUtils.tokenizeToArray( emailsString, ";" );
			for each (var email:String in emails)
			{
				var emailResult:Array = super.doValidation( email );
				if (emailResult.length > 0)
				{
					result = result.concat( emailResult );
				}
			}
			return result;
		}
	}

I first use the tokenizeToArray function (from the excellent as3commons lang) to split up the string into separate email addresses. Each of those are validated using the flex EmailValidator. All those results are put together and returned to the caller of doValidation().

The correctness of this is easily validated by writing a unit test:

	import flexunit.framework.TestCase;

	import mx.events.ValidationResultEvent;

	public class CompositeEmailValidatorTest extends TestCase
	{
		public function CompositeEmailValidatorTest()
		{
		}

		public function testSingleEmail():void
		{
			var validator:CompositeEmailValidator = new CompositeEmailValidator();
			var result:ValidationResultEvent = validator.validate( "wdb@company.com" );
			assertEquals( ValidationResultEvent.VALID, result.type );
		}

		public function testSingleEmailInvalid():void
		{
			var validator:CompositeEmailValidator = new CompositeEmailValidator();
			var result:ValidationResultEvent = validator.validate( "wdbcompany.com" );
			assertEquals( ValidationResultEvent.INVALID, result.type );
		}

		public function testMultipleEmails():void
		{
			var validator:CompositeEmailValidator = new CompositeEmailValidator();
			var result:ValidationResultEvent = validator.validate( "wdb@company.com;tl@company.com" );
			assertEquals( ValidationResultEvent.VALID, result.type );
		}

		public function testMultipleEmailsInvalid():void
		{
			var validator:CompositeEmailValidator = new CompositeEmailValidator();
			var result:ValidationResultEvent = validator.validate( "wdb@company.com;tlcompany.com" );
			assertEquals( ValidationResultEvent.INVALID, result.type );
		}
	}
Advertisements

3 thoughts on “Validation of multiple email addresses in flex

  1. The problem with this that while foo@company.com;bar@company.com comes back as valid, if the control is implemented, it still highlights in red and displays the tooManyAtSignsError message.

  2. Thanks, works well. Only issue I’m having is getting it to return an INVALID event when leaving the TextInput empty. Are you having the same issue?

  3. This works perfect. Thanks. Pretty simple solution, and saves me having to use a RegEx to do it. We already had the EmailValidator, so the extension was all that was needed.

    I a

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s