Rob Gonda's Blog

actionscript regular expression class

I still can't believe that Flash doesn't have a built-in regEx match or replace engine, but it can still be accomplished by using external classes. After vast research, I found a class that I really liked and I wrote a VERY simple example validating an email string.

This library is extremely useful because now you can apply patterns to validate numbers, dates, emails, urls, phones, etc. Most actionscripters are not hip or even aware that this is possible, and have to write dozens of lines to validate a simple email address.

You can download the class and the Flash test files, enclosed with this post.

Enjoy!

TrackBacks
There are no trackbacks for this entry.

Trackback URL for this entry:
http://www.robgonda.com/blog/trackback.cfm?B925AD80-3048-7431-E437088F37B5F731

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
This function alternates between working and being broken on consecutive calls. For example:
var email_re = new RegExp('^([\\w\\-\\.]+)@(([\\w\\-]{2,}\\.)+[\\w\\-]{2,3})$', 'gim'),

trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));

returns:
true
false
true
false
true
false

Can someone confirm that I'm not going crazy.
# Posted By Micah | 8/1/08 9:51 AM
@Micah (and also @Rob Gonda)


I have a feeling that the flags property has something to do with this.

I used a setInterval() call to simulate what Micah did:


import RegExp;

var emailPattern_str:String = '^([\\w\\-\\.]+)@(([\\w\\-]{2,}\\.)+[\\w\\-]{2,3})$';
var flags:String = 'gim'; // global, case Insensitive, multiline
var email_txt:String = 'rob@ichameleongroup.com';

var email_re = new RegExp(emailPattern_str, flags);
setInterval(traceit, 500, email_re);

function traceit(ere)
{
   trace(ere.test(email_txt));
}


this traces out:
true
false
true
false
true
false
...

However if I set the flags-property to an empy string "" instead of "gim",

i get
true
true
true
true
true
true
true
true
true
...


My guess is that Rob never intended the check to run twice, hence not making this a bug. My car makes a hell of a lot of noise when I turn the ignition key when the engine is already running. It's just a matter of how things are intended to be used. It works the first time, that's the most important part.

Kudos to Rob for writing this beautiful class for those among us who are sometimes forced to still use AS2.

Best regards,
Marvelade
# Posted By Marvelade actionscript & PHP lab | 8/16/08 5:57 AM
@Marvelade, thanks for clarifying it for the group.

@Micah, are you still using AS2? Hehe, I thought this was way gone :)
# Posted By Rob Gonda | 8/18/08 11:28 PM
Yes I'm using actionscript 2. Also a lot random expressions I try using don't work at all for example this one for a phone number I got from regexlib.com:
^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$
Any ideas?
# Posted By Micah | 8/28/08 2:21 PM
Rob,

It seems that if I use rob.gonda@ichameleongroup.com it traces false. I'm just getting started with regular expressions so I can't correct the code yet. Is there a way to allow for the extra "." in the email address?

Thanks.
# Posted By rgreen | 9/3/08 7:55 AM
This blog is running version 5.9.003. Contact Blog Owner