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