Form Validation with RegExp in AS3
November 7, 2007 08:41 am
RegExp is a new native class for ActionScript 3 that lets you work with regular expressions, which are patterns that you can use to perform searches in strings and to replace text in strings. Natively supported by many programming languages, ActionScript 3.0 implements regular expressions as defined in the ECMAScript edition 3 language specification (ECMA-262). Specifically for Flash or Flex development, RegExp will most likely be useful for any data validation or search and replace any string.
For example, you would like to verify a string contains the letters “AS”, you would do
[ftf auto=”true” h=”50″]var pattern:RegExp = /AS/;
trace(pattern.test(”AS3″)) //true[/ftf]
The regular expression to see if a string contains the letter “AS” is /AS/ (remember the ‘/’ before and ‘/’ after the expression), and the above function defines a new RegExp Object and test it against a variable called sString.
There are a lot more complicated regular expression syntax, you can start from the Adobe AS3 LiveDocs here to learn more. Here’s a class we made to do simple form validation for AS3:
[ftf]/**
* Various Validation Methods for Forms
* @author Ming Chan (ming_chan@the1stmovement.com)
* @version 0.1
* @usage var formValidation:Validation= new Validation();
trace(formValidation.isValidName(”Name”))
*/
package com.the1stmovement.form {
public class Validation {
public function Validation() {
}
/**
* Validate not empty - has at least one non-whitespace character
* @param sString
* @return true or false
*/
public function isNotEmpty(sString:String):Boolean{
var pattern:RegExp = /^\S{1,}$/
return pattern.test(sString);
}
/**
* Validate Name - The name can contain only alphabets(in either case) and should be of minimum length 3 with no maximum characters. Only white spaces are allowed apart from alphabets.
* @param sName:String Name
* @return true or false
*/
public function isValidName(sName:String):Boolean{
var pattern:RegExp = /^([a-zA-z\s]{3,})$/
return pattern.test(sName);
}
/**
* Validate US State - either in upper case or lower case 2 letter abbreviation
* @param sState:String State
* @return true or false
*/
public function isValidUSState(sState:String):Boolean{
var pattern:RegExp = /^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]))$/i
return pattern.test(sState);
}
/**
* Validate US Zip code - match either a 5 digit ZIP code or a ZIP+4 code formatted as 5 digits, a hyphen, and another 4 digits. (34564-3342 or 90210)
* @param sZip:String Zip Code
* @return true or false
*/
public function isValidUSZip(sZip:String):Boolean{
var pattern:RegExp = /^\d{5}(-\d{4})?$/
return pattern.test(sZip);
}
/**
* Validate US Phone Number - allowing user to enter 10 digit telephone number with segments of number separated by hyphens, periods or spaces. Also braces allowed around area code. (213-123-1234 2131231234 (213) 123-1234 213.123.1234)
* @param sPhone:String Phone Number
* @return true or false
*/
public function isValidUSPhone(sPhone:String):Boolean{
var pattern:RegExp = /^(\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?\d{3}(-|.|\s)?\d{4}$/
return pattern.test(sPhone);
}
/**
* Validates 1 or more email addresses. Email addresses can be delimited with either comma or semicolon. White space is allowed after delimiter, but not necessary.
* @param sEmails:String email addresses
* @return true or false
*/
public function isValidEmail(sEmails:String):Boolean{
var pattern:RegExp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*/
return pattern.test(sEmails);
}
}
}[/ftf]

