CFLib.org – Common Function Library Project

isURL(stringToCheck)

Last updated August 5, 2010

author

Nathan Dintenfass

Version: 2 | Requires: CF7 | Library: StrLib

Description:
This is a quick way to check if a string is a URL -- handy when, for instance, a user is entering a URL into a form that will later be used to make a link on a web page. Mostly just more convenient than needing to remember the regex. Notice that it makes use of isValid. The isValid function has built in URL checking, but this regex is considered to be stronger. Credit for the regex comes from DragingFireball.net: http://daringfireball.net/2010/07/improved_regex_for_matching_urls

Return Values:
Returns a boolean.

Example:

<cfset list = "www.foo.com,http://www.foo.com,http://intranet/foo/foo.htm,http:/noslash.com">
<cfoutput>
<cfloop list="#list#" index="s">
#yesNoFormat(isURL(s))# - #s#<br>
</cfloop>
</cfoutput>

Parameters:

Name Description Required
stringToCheck The string to check. Yes

Full UDF Source:

/**
 * A quick way to test if a string is a URL.
 * Regex by Gruber: http://daringfireball.net/2010/07/improved_regex_for_matching_urls
 * 
 * @param stringToCheck 	 The string to check. (Required)
 * @return Returns a boolean. 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 2, August 5, 2010 
 */
function isURL(stringToCheck){
       var URLRegEx = "(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»“”‘’]))";
       return isValid("regex", stringToCheck, URLRegex);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson