From 2803ea5fe911512609108cae6106ec0be301a4ac Mon Sep 17 00:00:00 2001 From: imdkbj <46839821+imdkbj@users.noreply.github.com> Date: Wed, 30 Sep 2020 00:45:01 +0530 Subject: [PATCH] Added isURL URL validation. --- validate.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/validate.js b/validate.js index 762a2fb..c2c0f4f 100644 --- a/validate.js +++ b/validate.js @@ -22,6 +22,8 @@ var alpha = /^[A-Z]+$/i, semver = /^v?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$/, unixPath = /^((?:\/[a-zA-Z0-9\.\:]+(?:_[a-zA-Z0-9\:\.]+)*(?:\-[\:a-zA-Z0-9\.]+)*)+\/?)$/, winPath = /^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$/; + url = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/g; + // https://en.wikipedia.org/wiki/Universally_unique_identifier#Definition var uuid = { @@ -1797,6 +1799,25 @@ function isRGBColor(str) { return rgbColor.test(str); } +// ************************************************************************************************ +// +// isURL +// +// Check if the string is a URL +// +// Examples : +// isURL('https://google.com') //returns true +// isURL('http://google.com/abcdxyz') //returns true +// isURL('xyz') // returns false +// +// ************************************************************************************************ +function isURL(str) { + if (isString(str)) + return url.test(str); + + return false; +} + // ************************************************************************************************ // @@ -1892,7 +1913,8 @@ exports = module.exports = { isWhiteSpace : isWhiteSpace, isWinFilePath : isWinFilePath, objectEquals : objectEquals, - startsWith : startsWith + startsWith : startsWith, + isURL : isURL }; // @@ -2019,4 +2041,4 @@ var SYSTEM_ERROR_CODES = { 'EWOULDBLOCK' : true, 'EXDEV' : true, 'EXFULL' : true -}; \ No newline at end of file +};