isHostname
Checks whether a value is a syntactically valid DNS hostname (RFC 952, RFC 1123, RFC 3696 section 2).
Rules:
- ASCII only: letters, digits and hyphens, case-insensitive.
- Labels are separated by dots and are 1 to 63 characters long. A label never starts or ends with a hyphen.
- The whole name is at most 253 characters (not counting an optional trailing dot).
- A single label is valid (
localhost). - The last label (the TLD) is never all digits (RFC 3696), so
192.168.0.1and123are not hostnames. Other labels may be all digits. - A trailing dot is refused unless
allowTrailingDotistrue. _is refused unlessallowUnderscoreistrue; then it is accepted anywhere in a label, including at the start (_dmarc) and the end. The hyphen rules still apply.- Unicode labels are refused. Punycode (
xn--...) is ASCII and accepted. To check an internationalised name, convert it first withnew URL('https://' + name).hostname. - Whitespace, a port, a path or a scheme (
example.com:80,a/b,http://x) make it invalid.
It only checks the syntax: it does not resolve the name and does not check that the TLD exists. It never throws and runs in linear time.
Available since v3.1.10
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | unknown | The value to check |
options | IsHostnameOptions | Optional switches for the trailing dot and the underscore |
Returns
Section titled “Returns”value is string — true if value is a string that is a valid hostname
Examples
Section titled “Examples”Validate a hostname typed by a user
Section titled “Validate a hostname typed by a user”Accepts DNS hostnames and single labels; refuses IPv4 addresses, ports and schemes.
Accept a trailing dot or an underscore on demand
Section titled “Accept a trailing dot or an underscore on demand”Fully qualified names and service names such as _dmarc are refused unless the option is set.
Check an internationalised name
Section titled “Check an internationalised name”Unicode labels are refused; convert them to punycode with URL first.
Related Types
Section titled “Related Types”IsHostnameOptions
Section titled “IsHostnameOptions”Options for isHostname.
