Building a Better Email Validator in Flash

Sep
16

Posted by Anthony in Flash, Web Design tagged , , ,

I work on a lot of Flash projects that require form validation. One issue that keeps coming up is the process of field validation, and more specifically, email validation. There are numerous rules that people use to validate email addresses (does it contain an @ sign, does it contain a dot, how many characters are there, etc.), but there is a fine line between overly strict validation and validation that’s way too loose. So I did some investigating about what makes up an email address, and how to best validate an email address in Flash. The result of my work is a robust EmailValidator class that I authored, written in ActionScript 3.0.

 

The Validation Logic

Here’s a quick rundown of what this EmailValidator class validates:

  • Must contain three parts: [1] Local part, [2] @ sign, [3] Domain
  • Local part must contain at least one of the following valid characters:
    • Upper and lowercase letters (A-Z, a-z)
    • Numeric Digits (0-9)
    • Other characters: ! # $ % & ‘ * + – / = ? ^ _ ` { | } ~
    • Periods (provided it is not the first or last character, and does not appear more than two times consecutively)
  • Domain must contain at least one period, as well as at least one character before the period as well as a valid top-level domain (i.e. com, org, ca, uk) after the last period. Valid domain characters include:
    • Upper and lowercase English letters (A-Z, a-z)
    • Numeric Digits (0-9)
    • Hyphens
    • Periods (provided it is not the first or last character, and does not appear more than two times consecutively)

With that list of validation logic written, I must also confess that, technically speaking, quoted strings and spaces (so long as they are within quotes) are valid in the local-part of an email. However, on the Internet Engineering Task Force (IETF) website, RFC 5321 Section 4.1.2 contends that,

“…a host that expects to receive mail SHOULD avoid defining mailboxes where the Local-part requires (or uses) the Quoted-string form…”

Since the practice of using quoted strings is discouraged by RFC 5321, the logic in my EmailValidator class does NOT allow quoted strings, including the double-quote character and spaces.

 

An Example

I have included a small Flash email validator file below that you can test the validity of an inputted email address.  It utilizes the EmailValidator class that I have written.  Feel free to test out a range of email addresses.  Note that the validator gives useful feedback about invalid addresses, specifying what the first encountered issue is when checking whether or not the address valid.

 

 

Known Issues & Exceptions:

  1. Many email providers out there will only accept letters, digits and hyphens as valid in the local part of the email. Though this is becoming more common, the EmailValidator class will still allow any of the valid characters listed above, even if it results in an invalid email address for a specific email client (i.e. */+@gmail.com). This is done to follow the direction of RFC 5322, and the list of valid atom characters listed in Section 3.2.3 that can be included in the local part of an email. Until this list is deprecated (if ever), it’s an exception I’m willing to live with.
  2. Though the EmailValidator checks for a valid domain (including only alpha characters, numbers and hyphens, as well as valid top-level domain), there’s no way to tell whether the domain actually exists. (For example, me@fake.net will validate, though www.fake.net may not be a valid site/domain.)

 

Resources:

Internet Engineering Task Force (IETF)
Internet Assigned Numbers Authority (IANA)

 

Interested in getting this EmailValidator class for your own use? Contact me to request a copy.

There are no comments yet, add one below.

Leave a Comment