Skip to content

Instantly share code, notes, and snippets.

@TonyChangho
Last active April 19, 2020 18:19
Show Gist options
  • Save TonyChangho/e53f06d2ee103f4ea030cd97527a002f to your computer and use it in GitHub Desktop.
Save TonyChangho/e53f06d2ee103f4ea030cd97527a002f to your computer and use it in GitHub Desktop.
JavaScript regex code for validating fully-qualified domain names per RFCs 1034 and 1123
/*
Per https://www.ietf.org/rfc/rfc1034.txt, Section 3.5. and https://tools.ietf.org/html/rfc1123, Section 2.1:
- Domains (whether TLD or subdomains) consist of labels, separated by dots when there's more than one (e.g., gist.github.com)
- Labels must start and end with a letter or a digit, and can have zero or more letters, digits, or hyphens between (e.g., the
following are all valid: a.com, aa.com, a1.com, 1.com, 11.com, 1a.com, a-a.com, a-1.com, 1-a.com, 1-1.com, a--a.com)
- Labels can only be a max of 63 characters
- Domains can only be a maximum of 255 characters
So, we'll look for a TLD label, and then in front of that, one or more subdomain labels trailed with a dot, with no more than 63
characters for each label, and no more than 255 characters for the whole string.
*/
function validateDomain(domain) {
const VALID_LABEL = "[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?";
const VALID_DOMAIN = `^(${VALID_LABEL}\\.)+${VALID_LABEL}$`;
const domainMatcher = new RegExp(VALID_DOMAIN, "i");
const length = domain.length;
return length > 0 && length < 256 && domainMatcher.text(domain);
}
@TonyChangho
Copy link
Author

TonyChangho commented Oct 6, 2018

Note: This only takes into account what is technically possible per the spec, not what is actually done in practice, e.g., to date, there are no TLDs that consist solely of a single character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment