Validating Domain Names
Cal has validating email addresses down pat but I can never seem to find a respectable regular expression for validating the format of domain names (useful when you license software by domain). So using the list of generic and country-specific top level domains (combined list) provided by the Internet Assigned Numbers Authority I put this together:
It checks against all known tlds and ensures that the domain name begins and ends with an alphanumeric character, allowing for dashes and sub-domains. Single character domains are allowed (did you know PayPal owns x.com?) but maximum length restrictions are not enforced. Additional logic is required to prevent something like co.uk passing as a valid domain name or to confirm that the domain is actually hosted somewhere—but I’ll leave that as an exercise for the reader.