Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Added support to ipv4 and ipv6 on URL validation #67

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ export const validateUrl = (value, allValues) => {
const type = allValues.type;
if (allValues[type].urlType !== URL_TYPE.FULL_URL) return;
if (!value) return 'Required';
const isValidUrl = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/.test(
value
);
const regname = '((www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b)';
const ipv4 = '(((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))';
const h16 = '([0-9a-fA-F]{1,4})';
const ls32 = `((${h16}:${h16})|${ipv4})`;
const ipv6 = `\\[(`+
`((${h16}:){6}${ls32})|`+
`(::(${h16}:){5}${ls32})|`+
`(${h16}?::(${h16}:){4}${ls32})|`+
`(((${h16}:){0,1}${h16})?::(${h16}:){3}${ls32})|`+
`(((${h16}:){0,2}${h16})?::(${h16}:){2}${ls32})|`+
`(((${h16}:){0,3}${h16})?::${h16}:${ls32})|`+
`(((${h16}:){0,4}${h16})?::${ls32})|`+
`(((${h16}:){0,5}${h16})?::${h16})|`+
`((${h16}:){0,6}${h16})?::`+
`)\\]`;
const regexUrl = `^https?:\\/\\/(${regname}|${ipv4}|${ipv6})([-a-zA-Z0-9@:%_\\+.~#?&//=]*)$`;
const isValidUrl = new RegExp(regexUrl).test(value);
if (!isValidUrl) return 'Invalid URL';
};

Expand Down