CFLib.org – Common Function Library Project

isURL(stringToCheck)

Last updated August 5, 2010

Version: 2 | Requires: ColdFusion MX7 | Library: StrLib

 
Rated 5 time(s). Average Rating: 2.8

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson