DEV Community

James Moberg
James Moberg

Posted on

3

ColdFusion isDomain() UDF

Since Apility.io introduced a new "free" tier on 12/1/2019 that provides 40k API hits per month, I've decided to revisit and add support to my ColdFusion web application framework in order to better combat feedback form spam. I recently wrote a CFC, CFTag & API endpoint for CleanTalk, so adding support for Apility shouldn't be too difficult. Apility uses multiple sources and can test IP, email and domain names as well as return other data like WHOIS & geolocation.

In preparation for this, I wanted to make it so the function auto-detects what is being tested (IP, email or domain name), automatically performs the correct API call and returns the standardized pass/fail response.

ColdFusion has a built-in isValid() function that supports email, but there's no support for IP or domain name. CFLib provides an isIP() UDF and also has an isURL() UDF. The isEmail() UDF is very basic and Adobe's built-in isValid("email") is/was buggy. As a result, I prefer to use my own UDFs versus blindly trusting that Adobe won't change something or introduce a new version-specific bug. (For email, I use Dominic Sayer's isEmail Java library. This library is awesome & also performs an optional MX record test.)

I noticed that there wasn't anything available to test whether a string is a validly formatted domain name, so I thought I'd come up with something.

Alt Text

Enjoy!

Check it out on TryCF.com.

<!--- 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>
view raw isDomain.cfm hosted with ❤ by GitHub

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more