Fully qualified domain name validation

后端 未结 6 1059
醉话见心
醉话见心 2020-11-28 06:05

Is there a quick and dirty way to validate if the correct FQDN has been entered? Keep in mind there is no DNS server or Internet connection, so validation has to be done via

6条回答
  •  广开言路
    2020-11-28 07:04

    We use this regex to validate domains which occur in the wild. It covers all practical use cases I know of. New ones are welcome. According to our guidelines it avoids non-capturing groups and greedy matching.

    ^(?!.*?_.*?)(?!(?:[\w]+?\.)?\-[\w\.\-]*?)(?![\w]+?\-\.(?:[\w\.\-]+?))(?=[\w])(?=[\w\.\-]*?\.+[\w\.\-]*?)(?![\w\.\-]{254})(?!(?:\.?[\w\-\.]*?[\w\-]{64,}\.)+?)[\w\.\-]+?(?

    Proof and explanation: https://regex101.com/r/FLA9Bv/40

    There're two approaches to choose from when validating domains.

    By-the-books FQDN matching (theoretical definition, rarely encountered in practice):

    • max 253 character long (as per RFC-1035/3.1, RFC-2181/11)
    • max 63 character long per label (as per RFC-1035/3.1, RFC-2181/11)
    • any characters are allowed (as per RFC-2181/11)
    • TLDs cannot be all-numeric (as per RFC-3696/2)
    • FQDNs can be written in a complete form, which includes the root zone (the trailing dot)

    Practical / conservative FQDN matching (practical definition, expected and supported in practice):

    • by-the-books matching with the following exceptions/additions
    • valid characters: [a-zA-Z0-9.-]
    • labels cannot start or end with hyphens (as per RFC-952 and RFC-1123/2.1)
    • TLD min length is 2 character, max length is 24 character as per currently existing records
    • don't match trailing dot

    The regex above contains both by-the-books and practical rules.

提交回复
热议问题