Skip to main content

validate_hostname

Function validate_hostname 

Source
pub fn validate_hostname(s: &str) -> Result<(), ValidationError>
Expand description

Validate an RFC 1123 hostname: dot-separated labels, each label 1–63 chars containing only ASCII letters / digits / hyphens, no leading or trailing hyphen on any label, total length ≤ 253. Empty string is rejected.

Suited for admin form fields that take a server hostname / DNS name. Distinct from validate_url (which expects a scheme) and validate_ipv4_address (which is for literal IPs).

Examples:

  • validate_hostname("example.com") → Ok
  • validate_hostname("sub.example.co.uk") → Ok
  • validate_hostname("localhost") → Ok (single-label allowed)
  • validate_hostname("-bad.example.com") → Err (leading -)
  • validate_hostname("very-long-label-that-exceeds-the-sixty-three-character-limit-XX.com") → Err
  • validate_hostname("") → Err

§Errors

ValidationError { code: "invalid_hostname", ... }.