diff --git a/src/Components/CreateImageWizard/utilities/useValidation.tsx b/src/Components/CreateImageWizard/utilities/useValidation.tsx index 04a20b57..2307c6d2 100644 --- a/src/Components/CreateImageWizard/utilities/useValidation.tsx +++ b/src/Components/CreateImageWizard/utilities/useValidation.tsx @@ -140,10 +140,14 @@ export function useFirstBootValidation(): StepValidation { export function useHostnameValidation(): StepValidation { const hostname = useAppSelector(selectHostname); + // Error message taken from hostname man page (`man 5 hostname`) + const errorMessage = + 'Invalid hostname. The hostname should be composed of up to 64 7-bit ASCII lower-case alphanumeric characters or hyphens forming a valid DNS domain name. It is recommended that this name contains only a single label, i.e. without any dots.'; + if (!isHostnameValid(hostname)) { return { errors: { - hostname: 'Invalid hostname', + hostname: errorMessage, }, disabledNext: true, }; diff --git a/src/Components/CreateImageWizard/validators.ts b/src/Components/CreateImageWizard/validators.ts index 1e03f8e6..c2525d79 100644 --- a/src/Components/CreateImageWizard/validators.ts +++ b/src/Components/CreateImageWizard/validators.ts @@ -92,10 +92,14 @@ export const isNtpServerValid = (ntpServer: string) => { }; export const isHostnameValid = (hostname: string) => { - if (hostname !== '') { - return /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/.test( - hostname - ); + switch (true) { + case hostname.length > 63: + return false; + case hostname !== '': + return /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/.test( + hostname + ); + default: + return true; } - return true; };