Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active December 30, 2019 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/06c285c9f1e6bfcf3115dbbba0252c9a to your computer and use it in GitHub Desktop.
Save JamoCA/06c285c9f1e6bfcf3115dbbba0252c9a to your computer and use it in GitHub Desktop.
isDomain() - ColdFusion UDF to perform regex to determine string is a validly formatted domain name.
<!--- 20191230 isDomain() - Simple domain name regex
https://gist.github.com/JamoCA/06c285c9f1e6bfcf3115dbbba0252c9a
IDN support requires Unicode characters & punycode to be converted to its ASCII equivalent.
Regex from https://www.regextester.com/93928 (modified to support single letter hostname like "g.co")
Try it here: https://www.trycf.com/gist/06c285c9f1e6bfcf3115dbbba0252c9a
Blog entry: https://dev.to/gamesover/coldfusion-isdomain-udf-1k32
--->
<cfscript>
function isDomain(inputString){
return javacast("boolean", refindnocase("^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9]+([a-zA-Z0-9-_])*\.[a-zA-Z]{2,11}?$", javacast("string", arguments.inputString)));
}
</cfscript>
<cfset Tests = [
"domain.com",
"example.domain.com",
"example.domain-hyphen.com",
"www.domain.com",
"example.museum",
"g.co",
"dev-admin.dev-facebook-england.co.uk",
"http://example.com",
"subdomain.-example.com",
"example.com/parameter",
"example.com?anything",
"notvalid.com.",
".notvalid.com",
"-notvalid.com",
"notvalid.com-"
]>
<h2>Simple Domain Name Regex</h2>
<cfoutput>
<ol>
<cfloop array="#Tests#" index="thisTest">
<li>isDomain("#thisTest#") = #isDomain(thisTest)#</li>
</cfloop>
<ol>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment