Posted By

martinj on 02/08/09


Tagged

url regexp validator


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vip4lyf
Pingpongbal


URLValidator


 / Published in: ActionScript 3
 

URL: http://parallaxdenigrate.net/?p=587

Class to validate URLs with or without protocol prefixes. Checks for valid TLD.

  1. package no.martinjacobsen.utils.string{
  2.  
  3. /**
  4. * Copyright 2009 Martin Jacobsen.
  5. *
  6. * @langversion ActionScript 3.0
  7. * @playerversion Flash 10.0
  8. *
  9. * @author Martin Jacobsen
  10. * @since 2009-02-02
  11. * @version 1.0
  12. * @description : Utility class for finding, validating and transforming URLs.
  13. *
  14. * Several URLParsers exist, but those I found were all too generic,
  15. * mainly by being too restrictive (only accepting URLs with 'http' and .com | .org.| .net)
  16. * or too lax (accepting .egg, .bacon and so forth). Hence this class.
  17. *
  18. * The URLParsing logic here is a unholy matrimoby of RegExp and String utilities.
  19. * The ActionScript implementation of RegExp leaves something to be desired, and I decided
  20. * to store the TLDs in a Vector partly because I've been told that running through a Vector
  21. * is more effective than using RegExp when you're looking for exact matches and partly
  22. * because I just couldn't get the RegExp to work when it reached a certain length...
  23. * Might just be my incompetence.
  24. *
  25. * The usage is pretty self-explanatory, but to sum it up;
  26. *
  27. * var validator : URLValidator = new URLValidator();
  28. * validator.validate("http://google.com") // returns true;
  29. * validator.validate("google.com") // returns true unless protocolOptional is set to false;
  30. *
  31. * var a:Array = validator.find(longStringWithScatteredURLs) // returns an Array of objects
  32. * a[0].url // the URL.
  33. * a[0].startIndex // the startIndex of the URL in the text
  34. * a[0].endIndex // you get it...
  35. *
  36. * validator.tag(longStringWithScatteredURLs) // returns the original string
  37. * // with all URLs neatly <a href'ed
  38. * // optionally add tags and substitute text.
  39. *
  40. *
  41. * @license: Do what thou wilt shall be the whole of the law. (But, hey: Credit is always welcome.)
  42. *
  43. */
  44.  
  45.  
  46. public class URLValidator {
  47.  
  48. private var urlStructure : RegExp;
  49. private var domainList : Vector.<String>;
  50. private var legal : Vector.<String>;
  51.  
  52. public var trimHotChars : Boolean = true;
  53. public var protocolOptional : Boolean = true;
  54.  
  55.  
  56. public function URLValidator () {
  57. createRules();
  58. }
  59.  
  60.  
  61. public function validate(stringToValidate:String) : Boolean {
  62. var isURL : Boolean = false;
  63. stringToValidate.toLowerCase();
  64. if(domainsValid(stringToValidate)) {
  65. if (urlStructure.exec(stringToValidate) != null &&
  66. stringToValidate.length == urlStructure.exec(stringToValidate)[0].length){
  67. isURL = true;
  68. }
  69. }
  70. return isURL;
  71. }
  72.  
  73.  
  74. public function findValid(stringToValidate:String) : Boolean {
  75. var hasURL : Boolean = false;
  76. stringToValidate.toLowerCase();
  77. if(domainsValid(stringToValidate)) {
  78. if (urlStructure.exec(stringToValidate) != null){
  79. hasURL = true;
  80. }
  81. }
  82. return hasURL;
  83. }
  84.  
  85.  
  86. public function find(stringToSearch:String) : Array {
  87. var subToSearch : String = stringToSearch;
  88. var urlArray : Array = [];
  89. var searchPos : uint = 0;
  90. while(subToSearch.length > 3){
  91. if (urlStructure.exec(subToSearch) != null){
  92. var theURL : String = urlStructure.exec(subToSearch)[0];
  93. if(validate(theURL)){
  94. if(trimHotChars) theURL = trim(theURL);
  95. var o:Object = new Object;
  96. o.url = theURL;
  97. o.startIndex = stringToSearch.indexOf(theURL);
  98. o.endIndex = o.startIndex + theURL.length;
  99. urlArray.push(o);
  100. }
  101. var n : uint = subToSearch.indexOf(theURL) + theURL.length;
  102. subToSearch = subToSearch.substring(n);
  103. } else {
  104. subToSearch = "";
  105. }
  106. }
  107.  
  108. function trim(s:String) : String {
  109. for (var i:int = s.length; i > 0; i++){
  110. if(s.substr(s.length-1).match(/[\.\)?!\"",]/) != null){
  111. s = s.slice(0,s.length-1);
  112. } else {
  113. break;
  114. }
  115. }
  116. return s
  117. }
  118. return urlArray;
  119. }
  120.  
  121. public function tag( stringToSearch : String,
  122. startTags : String = "",
  123. endTags : String = "",
  124. linkText : String = null) : String {
  125.  
  126.  
  127. var urls:Array = find(stringToSearch);
  128. for each (var urlObj:Object in urls){
  129. var theURL : String;
  130. var originalURL: String;
  131. theURL = originalURL = urlObj.url;
  132.  
  133. var link : String = "";
  134. var start : String = startTags + "<a href='";
  135. var end : String = "</a>" + endTags;
  136.  
  137. if(theURL.search(/((http|https|ftp):\/\/)/) < 0){
  138. theURL = "http://" + theURL;
  139. }
  140.  
  141. if (linkText) link = linkText;
  142. else link = originalURL;
  143.  
  144. theURL = start + theURL + "'>" + link + end;
  145. stringToSearch = stringToSearch.replace(originalURL, theURL);
  146. }
  147.  
  148. return stringToSearch;
  149. }
  150.  
  151.  
  152. private function domainsValid(hasDomainString:String) : Boolean {
  153. var hasDomain : Boolean = false;
  154. for each(var item:String in domainList){
  155. var tld : RegExp = new RegExp("(\\"+item+")");
  156. if (hasDomainString.search(tld) >-1){
  157. var nextChar : String;
  158. nextChar = hasDomainString.substr(hasDomainString.search(tld) + item.length, 1);
  159. if(isLegal(nextChar)){
  160. hasDomain = true;
  161. break;
  162. }
  163. }
  164. }
  165.  
  166. function isLegal(q:String) : Boolean {
  167. var isLegit : Boolean = false;
  168. for each (var legalChar:String in legal){
  169. if(q == legalChar){
  170. isLegit = true;
  171. break;
  172. }
  173. }
  174. return isLegit;
  175. }
  176. return hasDomain;
  177. }
  178.  
  179.  
  180. public function createRules() : void {
  181.  
  182. if(protocolOptional)
  183. urlStructure = /(((http|https|ftp):\/\/)?([-a-z0-9]*\.+)?[a-zA-Z0-9]*\.[a-zA-Z]+)([\/&\?=\.;%\+\$@~][\S]*)?/
  184. else
  185. urlStructure = /(((http|https|ftp):\/\/)([-a-z0-9]*\.+)?[a-zA-Z0-9]*\.[a-zA-Z]+)([\/&\?=\.;%\+\$@~][\S]*)?/
  186.  
  187. domainList = new Vector.<String>();
  188. domainList.push (".ac",".ad",".ae",".aero",".af",".ag",".ai",".al",".am",".an",".ao",".aq",".ar",".arpa",".as",".asia",".at",".au",".aw",".ax",".az",".ba",".bb",".bd",".be",".bf",".bg",".bh",".bi",".biz",".bj",".bm",".bn",".bo",".br",".bs",".bt",".bv",".bw",".by",".bz",".ca",".cat",".cc",".cd",".cf",".cg",".ch",".ci",".ck",".cl",".cm",".cn",".com",".coop",".cr",".cu",".cv",".cx",".cy",".cz",".de",".dj",".dk",".dm",".do",".dz",".ec",".edu",".ee",".eg",".er",".es",".et",".eu",".fi",".fj",".fk",".fm",".fo",".fr",".ga",".gb",".gd",".ge",".gf",".gg",".gh",".gi",".gl",".gm",".gn",".gov",".gp",".gq",".gr",".gs",".gt",".gu",".gw",".gy",".hk",".hm",".hn",".hr",".ht",".hu",".id",".ie",".il",".im",".in",".info",".int",".io",".iq",".ir",".is",".it",".je",".jm",".jo",".jobs",".jp",".ke",".kg",".kh",".ki",".km",".kn",".kp",".kr",".kw",".ky",".kz",".la",".lb",".lc",".li",".lk",".lr",".ls",".lt",".lu",".lv",".ly",".ma",".mc",".md",".me",".mg",".mh",".mil",".mk",".ml",".mm",".mn",".mo",".mobi",".mp",".mq",".mr",".ms",".mt",".mu",".museum",".mv",".mw",".mx",".my",".mz",".na",".name",".nc",".net",".nf",".ng",".ni",".nl",".no",".np",".nr",".nu",".nz",".om",".org",".pa",".pe",".pf",".pg",".ph",".pk",".pl",".pm",".pn",".pr",".pro",".ps",".pt",".pw",".py",".qa",".re",".ro",".rs",".ru",".rw",".sa",".sb",".sc",".sd",".se",".sg",".sh",".si",".sj",".sk",".sl",".sm",".sn",".so",".sr",".st",".su",".sv",".sy",".sz",".tc",".td",".tel",".tf",".tg",".th",".tj",".tk",".tl",".tm",".tn",".to",".tp",".tr",".travel",".tt",".tv",".tw",".tz",".ua",".ug",".uk",".us",".uy",".uz",".va",".vc",".ve",".vg",".vi",".vn",".vu",".wf",".ws",".ye",".yt",".yu",".za",".zm",".zw");
  189.  
  190. domainList.sort(strLenSort);
  191. function strLenSort(x:String,y:String) : Number {
  192. if (x.length > y.length){
  193. return -1;
  194. } else {
  195. return 1;
  196. }
  197. }
  198.  
  199. legal = new Vector.<String>();
  200. legal.push(" ","/","~","*","?",".",",",";",":","-","%","=","_","$","&","'","@","!","+","");
  201.  
  202. }
  203. }
  204.  
  205. }

Report this snippet  

You need to login to post a comment.