ActionScript 3 URL validator class

by Martin on 2/02/2009

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.

There are 14 comments in this article:

  1. 11/05/2009daniel sovino says:

    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. 11/05/2009Martin says:

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

  3. 27/05/2009Martin says:

    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. 31/07/2009Alexander Rindo says:

    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. 5/08/2009Martin says:

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

  6. 1/09/2009Mike says:

    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. 3/09/2009Martin says:

    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.

  8. 7/10/2010Emailadresse extrahieren !? - Flashforum says:

    [...] [...]

  9. 23/02/2011David says:

    Please, can you make a similar function for action script 2?
    Thanks a lot!
    :)

  10. 23/02/2011Martin says:

    David: No. No way. Never.

  11. 3/03/2011J says:

    This is to David.
    as2 is the devil. Don’t use it.
    Read this blog post for more on why you should never ever ever ever never ever use as2 if you can avoid it.
    http://intersectdigital.com/blog/2011/why-as3-gives-me-the-warm-fuzzies/

  12. 22/03/2011Thijs says:

    Thanks for this class, works great. Using it to process URLs in the messages of a chat application.

  13. 18/05/2011Human-style URL recognition « Control Option Command says:

    [...] made an honest attempt at resolving this problem with the URLValidator class back in february. The class performs pretty well at validating and finding URLs. However it works [...]

  14. 10/10/2011ronald says:

    its cool stuff god bless you

Write a comment: