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 regex/awk/sed.
Any ideas?
(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)
regex is always going to be at best an approximation for things like this, and rules change over time. the above regex was written with the following in mind and is specific to hostnames-
Hostnames are composed of a series of labels concatenated with dots. Each label is 1 to 63 characters long, and may contain:
Additionally:
some assumptions:
results: valid / invalid
EDIT: John Rix provided an alternative hack of the regex to make the specification of a TLD optional:
(?=^.{1,253}$)(^(((?!-)[a-zA-Z0-9-]{1,63}(?<!-))|((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63})$)
EDIT 2:
someone asked for a version that works in js.
the reason it doesn't work in js is because js does not support regex look behind.
specifically, the code (?<!-)
- which specifies that the previous character cannot be a hyphen.
anyway, here it is rewritten without the lookbehind - a little uglier but not much
(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)
you could likewise make a similar replacement on John Rix's version.
EDIT 3: if you want to allow trailing dots - which is technically allowed:
(?=^.{4,253}\.?$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}\.?$)
I wasn't familiar with trailing dot syntax till @ChaimKut pointed them out and I did some research
Using trailing dots however seems to cause somewhat unpredictable results in the various tools I played with so I would be advise some caution.