2021-05-23 09:27:51 +03:00
|
|
|
// to use with <input type="url"> fields, as the default pattern only checks for `:`,
|
|
|
|
export const DEFAULT_TEXTFIELD_URL_PATTERN = 'https?://.*';
|
|
|
|
|
2021-02-16 22:41:24 +03:00
|
|
|
export default function isValidUrl(url: string): boolean {
|
2021-02-16 22:24:54 +03:00
|
|
|
const validProtocols = ['http:', 'https:'];
|
|
|
|
|
|
|
|
try {
|
2022-05-12 09:31:31 +03:00
|
|
|
const validationObject = new URL(url);
|
|
|
|
if (
|
|
|
|
validationObject.protocol === '' ||
|
|
|
|
validationObject.hostname === '' ||
|
|
|
|
!validProtocols.includes(validationObject.protocol)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-02-16 22:24:54 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:41:24 +03:00
|
|
|
return true;
|
2021-02-07 07:07:01 +03:00
|
|
|
}
|