2021-05-22 23:27:51 -07: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 11:41:24 -08:00
|
|
|
export default function isValidUrl(url: string): boolean {
|
2021-02-16 11:24:54 -08:00
|
|
|
const validProtocols = ['http:', 'https:'];
|
|
|
|
|
|
|
|
try {
|
2022-05-11 23:31:31 -07:00
|
|
|
const validationObject = new URL(url);
|
|
|
|
if (
|
|
|
|
validationObject.protocol === '' ||
|
|
|
|
validationObject.hostname === '' ||
|
|
|
|
!validProtocols.includes(validationObject.protocol)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-02-16 11:24:54 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:41:24 -08:00
|
|
|
return true;
|
2021-02-06 20:07:01 -08:00
|
|
|
}
|