Advertisement
  1. Code
  2. ActionScript
Code

Validating Various Input Data in Flash

by
Difficulty:IntermediateLength:MediumLanguages:

Today, almost everything on the web is based on user input. A contact form, a user registration form, a search box and so on. As a developer you can't just rely on the user to write everything as it's supposed to be. To be sure you always get the correct data from your users you will need to validate the input. Read on to find out how..

In this tutorial you will learn how various data is supposed to be structured and how to validate this data. Note that these methods may not be the best out there or the most complete solutions.

The following topics will be covered in this tutorial:

  • Date and time validation
  • E-mail validation
  • Web address validation
  • Phone number validation
  • International Standard Book Number (ISBN) validation
  • International Bank Account Number (IBAN) validation
  • Credit card number validation

Each topic is handled independently so you can follow the ones you are interested in.


Final Result Preview

This is how our validator will look:

There are multiple fields with the sole purpose of demonstration. In a real scenario you will rarely have all these fields in a single form.

In this tutorial I will only cover the coding part. You will find the graphic interface in the source files. To follow along you will need Flash CS3+.

To better understand this tutorial you must have some basic knowledge about Regular Expressions and string functions. Please check this resource about regular expressions in ActionScript 3.0 if you don't feel comfortable with the syntax.

Understanding the .fla File

Well it's not a very complicated interface. If you open the Validator.fla file in the begin folder of the Source download, you will find two frames: one locked, named ActionScript and another one called Interface.

The Interface layer is currently empty. There we will add our text boxes used for the data input.

On the ActionScript layer you will find this code:

The first line represents an instance of our future Validator class. We'll use a separate AS file to contain this class, so that it can be used in other projects.

We add event listeners to the stage for focus events. We will use these events to validate our input data only when a text field has focus just to avoid unnecessary calculations.

The onFrameUpdate function is called on every frame when a text field has focus to validate the data in it.

We'll add our future code in the switch statement. Everything else remains unchanged over this tutorial. In the library you will find a MovieClip called TextBox. This has an input TextField with an instance name of input_txt and another MovieClip called validTick with two frames labeled "valid" and "invalid".

Now let's get to the coding.


Step 1: Preparations

First create a new ActionScript file and name it Validator.as

Paste or type in the following code:

This will be the framework of our Validator class. We will add methods to this class for each validation type we perform. Place this file in the same folder as the Validator.fla file.


Step 2: Calendar Date Structure

The calendar date is one of the most frequently used data on the web. The structure varies from zone to zone. We will look at two main dates formats: United Kingdom version and United States version.

  • The UK date is expressed as DD-MM-YYYY.
  • The US date is expressed as MM-DD-YYYY.

In both cases DD stands for the day, MM stands for the month and YYYY stands for the year. It's also possible to use another character for the separator, like a slash.

If you need further documentation you can learn more about calendar dates here.


Step 3: Calendar Date Check

Code

First of all we need to check to see if the date format is correct. Paste or type the following code in our Validator class just after the constructor:

Explanation

The first line in the function represents the pattern used to check the month:

This pattern will match a number from 1 to 12. For numbers under 10 it will check for digits with leading zero or without.

The second line represents the day pattern:

This will match a number from 1 to 31. As previously mentioned, for numbers under 10 it will check for digits with or without leading zero.

The next line represents the year pattern:

This will check for numbers between 1000 and 9999. Of course we could of used a pattern to check for years from 0 to more than 9999 but for the sake of ease we will check for numbers within these nine millennia.

The variable separator simply checks if one of these four separators is used: hyphen "-", dot ".", front slash "/" and white space " ".

The two variables usDate and ukDate represent the regular expressions used to check a date. They are the same basically the only difference being the order of the month and day.

This will check to see if a valid month is at the beginning of the string. After it checks if a valid separator is used then it checks for a valid day. Next it uses a backreference to the second group used in the expression (the separator) to check if the second separator was used. And lastly it checks for a valid year at the end of the string.

The ukDate expression has the same effect. The only difference is that it will check for the day at the beginning of the string.

In the last line we test the string using the ternary operator to see if it is a valid date in any of the two formats and returns true or false, depending on the case. In a real case scenario you wouldn't check for both these values as this can cause confusion if the DD is less than 13 (e.g. 01.06.2010).


Step 4: Calendar Date Implementation

Open the Validator.fla file provided in the source files. Select the Interface layer and place an instance of the TextBox movie clip on the stage. To do this simply drag it from the library anywhere on the stage. Give it an instance name of dateField.

img1

Note: If you can't see the library panel you can open it by going to Window > Library or by pressing CTRL+L

Using the Text Tool (Hotkey T) create a static text field just above the previously created instance of TextBox and write "Date:" in it.

img2

Select the ActionScript layer and open the Actions panel by pressing F9 or by going to Window > Actions. Type in the following code in the switch statement from the onFrameUpdate function on line 22:

This will validate the input using the validator instance as a date.

Test the movie by pressing CTRL+ENTER on the keyboard or going to Control > Test Movie. Enter a date in the text field in any format(UK or US) using the same separator between the month, day and year. If you entered a correct value the green tick should appear.


Step 5: E-mail Address Structure

An email address represents a reference to the electronic mail box where email messages are sent. The RFC specification states that an email address can be composed from the following:

  • Uppercase and lowercase English letters (a-z, A-Z)
  • Digits 0 to 9
  • Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~ .

Most likely you won't find an email provider which allows all these characters in the email address. To validate such an email address requires a long and very complicated regular expression. As many companies(e.g. Yahoo!, MSN, Gmail) allow only email addresses which contain alphanumeric characters and hyphen "-", dot "." and underscore "_", we will make a checker which we will work with these kind of addresses.

[Ed. Actually, Gmail addresses allow a + sign; we leave it as a challenge for you to implement that ;)]

An email address is usually formed from the "username"(or "address") an "@" sign and the domain(name and extension).

If you want to know more about e-mail addresses you can read the E-mail Address wiki page.


Step 6: E-mail Address Check

Code

Paste or type the following code in our Validator class just after the checkDate method:

Explanation

The first line defines the pattern used to check the actual 'address' or username. This checks to see if it contains only the legal characters:

The second variable defined represents the domain name pattern. A valid domain name and subdomain name contains only alphanumeric characters, the dot '.' and hyphen '-':

Next there is the domain extension pattern used to check if the domain extension is a valid one. A Top Level Domain(TLD) can only contain characters from A to Z, case insensitive with 2 or more characters(in our case 6):

To make the check even more strict you can check if the TLD is an existing one:

These are just a few generic TLDs. You can see how this becomes quite long as you add more and more TLDs.

The email variable represents the Regular Expression used to test if the email address is a valid one. This checks if a valid username/address exists at the beginning of the email address, if it has the "a" sign somewhere in the middle after which it checks for a valid domain name and extension both separated by the dot "." Character:

Notice that we haven't used any uppercase letters. This is because we made use of the case-insensitive modifier "i" as generally email addresses are case-insensitive.


Step 7: E-mail Address Implementation

Note: For more detailed instructions see step 4.

Open Validator.fla, make a new instance of the TextBox movie clip and give it an instance name of emailField.

Create a static Text Field over the TextBox and write in it "E-mail:"

Type the following code on line 25:

This will check the input data in the emailField TextBox as an e-mail address using the validator instance of the Validator class.

Test the movie and enter an e-mail address in the text field labeled with "E-mail".


Step 8: Web Address Structure

Every time you surf the web you will most likely type a web address (URL) in your web browser to navigate to your desired location. Another place where you might use it is in a comment form where you are asked to type your website (address) or when manually submitting websites to a search engine.

A web address is made up of the following:

  • Protocol or scheme: http, https or ftp + "://"
  • Domain name: which can contain any alphanumeric character, dot "." and the hyphen "-"
  • Domain extension: made up of alphanumeric characters

For a more detailed documentation about Web Addresses(URLs) see this page.


Step 9: Web Address Check

Code

Paste or type the following code in our Validator class just after the checkEmailAddress method:

Explanation

The first line defines the pattern used to check the protocol. Usually this can be optional (depending on the case):

The second variable defined represents the domain name pattern. A valid domain name and subdomain name contains only alphanumeric characters, the dot "." and hyphen "-":

Next there is the domain extension pattern used to check if the domain extension is a valid one. A Top Level Domain(TLD) can only contain characters from A to Z, case insensitive with 2 or more characters(in our case 6):

To make the check even more strict you can check if the TLD is an existing one:

These are just a few generic TLDs. You can see how this becomes quite long as you add more and more TLDs.

The web variable represents the Regular Expression used to test if the web address is a valid one. This checks if the provided address is made up from the protocol (optional) and domain (the domain must be made up of a domain name and a domain extension separated by the dot "." character:

As in the case of the e-mail address web addresses are case insensitive so we use the "i"(case insensitive) modifier.


Step 10: Web Address Implementation

Note: For more detailed instructions see step 4.

Open Validator.fla, make a new instance of the TextBox movie clip and give it an instance name of webAddressField.

Create a static Text Field over the TextBox and write in it "Web address:".

Type the following code on line 28:

This will check the input data in the webAddressField TextBox as a web address using the validator instance of the Validator class.

Test the movie and enter a web address in the text field labeled with "Web address".


Step 11: Phone Number Structure

The most common place to use a phone number on the web is a contact form or SMS applications. A phone number is made up of the following two elements:

  • Country code: a number between 1 and 999 preceded by a plus "+" sign or double zeros "00" (optional).
  • Local phone number: made up of only digits. The number of digits depends on the operator, country and phone number type.

Here you will find a complete list of country calling codes if you would like to restrict your phone number input to only several countries. Also you can check the Numbering Plan to see how telephone numbers are used in different networks and countries.


Step 12: Phone Number Check

Code

Paste or type the following code in our Validator class just after the checkWebAddress method:

Explanation

The first line defines the pattern used to check the country code. As said this is a number between 1 and 999 preceded by "+" or "00"(optional):

The second variable represents the actual number. We will make it to check for a number with 3 to 10 digits:

On the third line we strip our phone number of any extra spaces or separators keeping only the "+" sign and digits:

The phone Regular Expression is the expression used to check if the phone number is made out of a country code and a local number:

The last line returns the result of testing the pattern over the provided phone number. Easy enough, right?


Step 13: Phone Number Implementation

Note: For more detailed instructions see step 4.

Open Validator.fla, make a new instance of the TextBox movie clip and give it an instance name of phoneNumberField.

Create a static Text Field over the TextBox and write in it "Phone number:".

Type the following code on line 31:

This will check the input data in the phoneNumberField TextBox as a phone number using the validator instance of the Validator class.

Test the movie and enter a phone number in the text field labeled with "Phone number".

Well this is what you should have until now:

And this is the code:

Compare your results with mine and make sure everything is where is supposed to be. Now... let's move on!


Step 14: ISBN-10 Structure

International Serial Book Number, or for short ISBN, is an international identification number given to a book. ISBN numbers are unique and are only give once to a book. Until now we couldn't check if the data was actually valid but ISBN numbers have a control digit which needs to be verified with a checksum algorithm.

The Checksum Algorithm:

  • We strip the number of slashes or spaces and store the last digit separately (this is the control digit).
  • We multiply every digit in the 9 digits number with his weight. The weights range from 10 to 2 starting from the first digit of the number.
  • We sum the resulted numbers and we extract the remainder for the division of the number with 11.
  • If the remainder is 0 the control character must be 0. If the remainder is not 0 we substract it from 11 and if the result is 10 the control character must be "x" else the control character must match the result.

If you need more detailed specifications about ISBN numbers you can find all about them Here. The wiki page contains details about both ISBN-10 and ISBN-13 numbers.


Step 15: ISBN-10 Check

Code

Paste or type the following code in our Validator class just after the checkPhoneNumber method:

Explanation

The first lines in our method strip the number of any spaces and hyphens. Then it checks if the length is correct. An ISBN-10 number must have 9 digits + a control character:

In the else the variable valid stores the validity of the number and weights is an array with the weights of the digits:

In the digits array we store the digits of the number and extract the last character which is the control character and store it in the control variable. The result variable will store the result of the check sum:

Now that we have every digit in the number we must multiply it by its weight. We can do this with a for-loop in which we multiply every element (digit) in the digits array with its corresponding weight and store the sum of these multiplications:

In the next line of code we check the remainder of the sum with respect to 11:

This could have been done with an if statement but I've used the ternary operator which is a shorthand method. The ternary operator works like so variable = condition ? value1 : value2;. If the condition is true value1 will be given to the variable else it will receive value2.

Next we check the result with a switch statement and give the value to the valid variable. If it's 10 then the the control character must be "x" else the result must match the corresponding digit:

And lastly we return the result of the test.


Step 16: ISBN-10 Implementation

Note: For more detailed instructions see step 4.

Open Validator.fla, make a new instance of the TextBox movie clip and give it an instance name of ISBNField.

Create a static Text Field over the TextBox and write in it "ISBN number:".

Type the following code on line 34:

This will check the input data in the ISBNField TextBox as a ISBN-10 using the validator instance of the Validator class.

Test the movie and enter a ISBN-10 number in the text field labeled with "ISBN number".

Be aware that validating an ISBN-10 number doesn't necessarily mean that this exists or belongs to any book.


Step 17: ISBN-13 Structure

"What? Another ISBN number?" you might say. Well, actually the previous number format is an old standard. This new ISBN-13 standard was introduced in 2005 and until 01.01.2007 the both formats were used for books. The ISBN-13 format has 12 digits and a thirteenth digit being the control digit.

The Checksum Algorithm:

  • We strip the number of spaces and hyphens.
  • We multiply every digit with its corresponding weight. The weights are 1 and 3 and are distributed like so:
    tabel1
  • We sum the resulted numbers and we extract the remainder for the division of the number with 10.
  • If the remainder is 0 the control character must be 0. If the remainder is not 0 we substract it from 10 and the result must match the control character.

Step 18: ISBN-13 Check

Code

Paste or type the following code in our Validator class just after the validateISBN10 method:

Explanation

The digits array contains the digits of the number. We do this by extracting the digits from our number using the match method of the String class. After that we store the control digit in the control variable by extracting the last element in the digits array:

The weight variable will hold the current weight of a digit and the result variable will hold the result of the checksum:

We use an in-else statement to see if there is the correct number of digits in the digits array, excluding the control digit.

In the for-loop we iterate through the digits array and multiply every digit with its weight (1 and 3 alternating them every digit) and store the sum in the result variable. If the position of the digit in the array is an even number the weight is 1 else the weight is 3. We do this by checking the remainder for the division of i by 2:

Lastly we make division by 10 of the sum. If it's 0 the result is 0 else we substract the remainder from 10:

If the result digit is the same as the control digit the number is valid.


Step 19: ISBN-13 Implementation

Open Validator.fla and modify the code on lines 34 to 36 like so:

This will check the number both as ISBN-10 and ISBN-13.

Test the movie and enter an ISBN number in the text field labeled with "ISBN number". The same goes for this type of number as in the case of ISBN-10 ones. If a number is valid it doesn't necessarily mean that it also exists.


Step 20: IBAN Structure

International Bank Account Number or IBAN for short is an international standard for numbering bank accounts. This is made up of the following:

  • Two alphabetic characters (uppercase) representing the country
  • A check number made up of two digits ranging from 00 to 99
  • Four alphabetic characters (uppercase) representing the bank who issued the number
  • An account number of various lengths depending on the country and bank

The Validation Algorithm:

  • We strip the number of spaces.
  • The first four characters in the number (country characters and control digits) are moved to the back of the number.
  • We convert all alphabetic characters in numbers like so:
    A = 10, B = 11, C = 12, ..., Z = 35.
  • We compute the remainder of the division by 97.

The number is valid only if the remainder is 1.

Check this page for a complete documentation on International Bank Account Numbers.


Step 21: IBAN Check

Code

Paste or type the following code in our Validator class just after the validateISBN13 method:

Explanation

The nums object contains the corresponding number for each character:

The chars array contains the characters in the number:

With the first for-loop we move the first four characters to the back by removing them from the beginning of the array and placing them to the back:

The exp regular expression is a simple pattern for alphabetic characters used to test every element in the chars array:

In the second for-loop we check every character in the chars array to see if it's a letter and change it to its corresponding number using the nums object:

After converting every letter in a number we transform the array into a string and assign it to the iban variable:

Lastly we return if the remainder of the division by 97 of the number is equal to 1 or not.

You might wonder why I've used a custom function instead of the "%" operator. Well… if you think about it, the number has 20+ digits (very large) and the normal modulus operator will not work as expected. So we need to define a way to calculate the modulus for large numbers.

The Modulus Function

To calculate the modulus of large numbers I've used an implementation of the "divide and conquer" method. To keep it short this method substracts a part of the large number and makes the modulus of that part after which it appends the result to the remaining portion of the large number. For a more detailed presentation of this method see This link.

Paste or type the following code in our Validator class just after the validateISBN method:

The tmp variable stores a part of the large number by substracting 10 characters from it:

The number variable represents the large number which gets trimmed by the length of the tmp string:

The result variable will obviously hold the result of the division. In the do-while loop we compute the remainder for the division of the tmp number by the mod integer (the divider) and assign the value to the result:

We add the result to the beginning of the large number and repeat the previously three steps while the number length is larger than 0:

And lastly we return the result of the modulus.


Step 22: IBAN Implementation

Note: For more detailed instructions see step 4.

Open Validator.fla, make a new instance of the TextBox movie clip and give it an instance name of IBANField.

Create a static Text Field over the TextBox and write in it "IBAN number:".

Type the following code on line 37:

This will check the input data in the IBANField TextBox as an IBAN using the validator instance of the Validator class.

Test the movie and enter an IBAN number in the text field labeled with "IBAN number".


Step 23: Card Number Structure

On the front of every credit or debit card you will find a number which varies in length. You use this number usually to make online payments using your credit card. As I said, the number varies in length and format depending on the company which it belongs to. But they all have one thing in common: they must pass a checksum algorithm. Credit cards numbers are validated using the Luhn (mod 10) algorithm.

Before we dive into the actual validation we need to check if the number belongs to any valid company. We will check for these three main card issuers: American Express, Dinners Club, MasterCard and Visa. The following table contains the number details for each company:

tabel2

The IIN Range is the number which the card number begins with.

The Luhn Algorithm

  • We double every second digit in the number starting from the first digit if the number has an even number of digits or starting from the second digit otherwise.
  • We sum the digits of the product together with the undoubled digits.
  • We check if the total sum modulo 10 is equal to 0 (or the number ends in 0) the number is valid.

For further reading see the Bank Card Number and the Luhn Algorithm wiki pages


Step 24: Card Number Check

Code

Paste or type the following code in our Validator class just after the validateIBAN method:

Explanation

The americanExpress regular expression defines the pattern used to check American Express card numbers. This will check for numbers beginning with 34 or 37 and has 13 more digits:

The dinnersClub regular expression defines the pattern used to check Dinners Club card numbers. This pattern checks if a credit card number begins with a number ranging from 300 to 305 and has 13 more digits:

The masterCard regular expression defines the patter for MasterCard card numbers. This pattern will check if the credit card number begins with a number between 51 and 55 and ends with 14 more digits:

The visa regular expression defines the pattern for Visa/Visa Electron card numbers. This will check if the provided card number begins with 4 and ends with 12 or 15 mode digits:

We use the extended modifier ("x") in every expression so that whitespaces will be ignored from. This is used just to make the expression easier to read.

The Boolean variable valid will hold the validity status of the card number.

On the sixth line we extract the digits from our number (the number is usually written with hyphens or spaces for readability purposes):

In the if statement we check if the number matches any of the four patterns defined earlier:

On the last line we return true if the number is valid and passes the Luhn checksum.

The Luhn Algorithm

Type in or paste the following code after the validateCardNumber method in our Validator class:

The digits variable contains the digits of the number. The start variable is used to define from which digit to start doubling (this is represented by the index in the digits array). The sum variable holds the total sum. We use the while loop to double every second digit in the array. We also add 2 to the start variable to skip at every second digit in the array:

On the next line we transform the digits array into a string and split it again into an array. We do this because if the doubled numbers are bigger than 9 we must sum the digits (e.g. for 12 we do 1+2):

In the for-loop we simply sum all the digits:

And lastly we check if the remainder is 0 and return true or false otherwise:


Step 25: Card Number Implementation

Note: For more detailed instructions see step 4.

Open Validator.fla, make a new instance of the TextBox movie clip and give it an instance name of cardNumberField.

Create a static Text Field over the TextBox and write in it "Card number:".

Type the following code on line 40:

This will check the input data in the cardNumberField TextBox as a Card Number using the validator instance of the Validator class.

Test the movie and enter a Card Number number in the text field labeled with "Card number".


Final code

Now this is how our final class looks:

I've added comments to the final result so that you remember how each method works and how the tested value should look like.

You can use this class in any of your AS3 projects.


Conclusion

Remember that even if you've validated your data this might not be real (as in the case of ISBNs, email addresses, IBANs etc) as many of these can be generated or random. But checking the data before registering it to your database, sending emails or submiting it to a payment server might catch some typos and get the user on the right track.

I hope this tutorial about various validations in ActionScript has helped you to understand the basics of input data validation. Next you can try to modify these methods and make them more restrictive, or more specific to your needs. Also you can try to make your own validation methods for any other data.

Thanks for reading my tutorial and please leave your feedback about it!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.