Using Regular Expressions to Validate Data (Part 2)

In this task, you will continue to use regular expressions and delve into some more complicated syntax. You will use regular expressions to be sure that an end user in the e-commerce application has entered a valid address. In reality, validating an address is difficult, there are many possible combinations; in this example you will verify that a street address starts with a number, contains a space, and then contains some more characters. This will work for the vast majority of the U.S. addresses.


Open BillingInfo.mxml in the views/ecomm directory.


On the first line of the process() method, create a new RegExp object with the name of pattern, as follows:

var pattern:RegExp = new RegExp(); 

This creates a new pattern object that can store regular expression strings for searching.


Inside the constructor for the RegExp() object, define the search pattern as two back-slashes and a d+.

var pattern:RegExp = new RegExp("\\d+"); 

The d indicates to search for a single digit. The + sign is referred to as a Quantifier; it works with the d to indicate that you will accept one or more digits as a match.


After defining the expression that searches for the beginning digits, add an escape character, the set of double backslashes, and add an x20 character that represents a space.

var pattern:RegExp = new RegExp("\\d+\\x20"); 

This indicates that a space is required between the digits and the letters of the address.


In brackets, check for all lowercase and all uppercase letters using A-Z and a-z. Add a plus sign after the brackets to allow for any length of those letters.

var pattern:RegExp = new RegExp("\\d+\\x20[A-Za-z]+"); 

This searches for any length of letters, in any case directly after the space. You have now built a pattern that will check for valid addresses!


Immediately after defining the pattern object, surround the rest of the method with conditional logic. Within the conditional logic, use the search() method of the String class to search the checkoutInfo.billingAddress with the regular expression pattern you defined in the last step. If the value is not equal to -1, execute the code.

if( != -1){    var o:ObjectDataEvent = new ObjectDataEvent ("billingInfoReturn",checkoutInfo);    dispatchEvent(o); } 

The search() method of the String class will search the entire checkout Info.billingAddress string for the regular expression that you defined earlier.


Add an else statement that will display an Alert box if -1 is returned. The final process() method should look as follows:

private function process():void{    var pattern:RegExp = new RegExp("\\d+\\x20[A-Za-z]+");    if( != -1){       var o:ObjectDataEvent = new ObjectDataEvent("billingInfoReturn",checkoutInfo);       dispatchEvent(o);    }else{"Please enter a valid US address");    } } 


Save and run the EComm application.

Type in some different combinations into the Address field. You should see that only those combinations that begin with numbers, have a space, and then a sequence of letters are accepted.

Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225 © 2008-2017.
If you may any questions please contact us: