ActionScript 3 URL validator class

A fairly common task in ActionScript projects is to deal with URLs in dynamically loaded text. Whenever I faced this prospect I usually ended up with some half-assed searching for “http://” and then indexOf(” “) to determine when the URL had ended and then smacked a <a href> on either side. Which pretty much meant that “http://___||/**.tasty_cheese_omelete” would be acceptable while “google.com” would not.

Hence I always promised myself that next time I’d write a proper class to deal with URLs so that this bullshit wouldn’t pass anymore.

Regular Expressions have always scared the bejeezus out of me, but I realized there was probably no other option when looking for something that has a valid URL structure. It was surprisingly easy to find some fairly good examples for what I wanted to achieve, and surprisingly difficult to get any of them to work in ActionScript. I’m blaming it on the AS implementation of RegExp, and I’m sure ActionScript is blaming it on my implementation of dumb.

After some swearing though I managed to determine whather a string had the proper structure of a URL but it still wasn’t perfect. I wanted my class to be intelligent enough to accept all valid Top Level Domains (there’s 267 in use) but reject any invalid attempt to pass as a valid TLD. IE: egypt.eg should pass, but breakfast.egg should not. The solution ended up being storing all the TLDs in a Vector and parsing through them to check if the domain was valid.

All in all the class now does a pretty good job, although there are probably still holes in it (let me know about them when you find them).

Check it out and grab it below, and then post a comment explaining to me slowly and carefully that I can achieve this exact functionality with the

1
String.giveMeAllTheURLs();

method or something similar, which I’m sure is what will happen. ;)

The code is freely available here. I would appreciate any credit and that you share whatever improvements you make with me and the rest of the world, but I have neither time nor energy surplus to actually do any enforcing, so knock yourself out.

Possibly related posts:

  1. Capture key sequences in ActionScript 3. AKA The Konami Comeback. Lately the internet has had a wave of sites...
  2. Macromedia ShockZone A while ago I was doing a talk on the...
  3. Twitter, Twitter, Twitter Update!! Yahoo did the same dick move of cockblocking Flash....
  4. TextMate + QuickSilver = Drag & Drop ActionScript imports. I couldn’t possibly code in anything but TextMate. Hell, I...
  5. Better (AS3) imports in TextMate Summary: In which Martin has concocted a hack to make...

7 Comments

  1. Posted May 11, 2009 at 06:05 | Link

    try this url:

    http://co-op.cl/

    false, and in fact it exists.
    I’m not a regex guy to solve this out….
    anyway, still pretty cool stuff..

    blesses

  2. Martin
    Posted May 11, 2009 at 16:07 | Link

    Thanks for pointing that out to me. I’ll have a looksie to see where I went wrong.

  3. Martin
    Posted May 27, 2009 at 11:40 | Link

    I fixed the issue with a dashed URL. It was a bug in the validate method, so the class would find the URL using the find() method but not properly validate it.

    Anyways; Fixed now. Keep the bugs coming. :)

  4. Alexander Rindo
    Posted July 31, 2009 at 21:08 | Link

    I’m posting this as a comment since I don’t know if you’re contact form actually works.

    I updated you source so that it handles multiple identical domains correctly, you can view the source here:

    http://alexrindo.com/URLTagger/URLValidator.as

    test it here:

    http://alexrindo.com/URLTagger

  5. Martin
    Posted August 5, 2009 at 09:21 | Link

    @Alexander: Thanks! I incorporated your changes in the version posted above.

  6. Mike
    Posted September 1, 2009 at 20:53 | Link

    Very good class! I have been searching all day for something that did just this thing! I do have a bug for you. It includes … in it’s searching sometimes so the phrase:

    thinking about taking a walk…don’t judge please

    Returns:

    FOUND URL: ‘walk…don’ at [24-33]

    It doesn’t validate it, but does find it.

  7. Martin
    Posted September 3, 2009 at 15:41 | Link

    Been a while since I looked at this now, but it sounds like the “find” method probably doesn’t really do a good job at validating the URLs on its own.

    I’ll have a look when I get a chance.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*