isURL(stringToCheck)
Last updated August 05, 2010
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);
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016