javascriptreactjsvalidation

Is it possible to validate and allow only https url's using .isUri using this library https://github.com/ogt/valid-url?


Is there a way to validate "https" url's using .isUri from this library https://github.com/ogt/valid-url in javascript (it should return true only for https url's)? (any other method should be fine except regex)

https://test.com //true
test.com // false
http://test.com // false
http://test // false

code snippet (below works great but it returns true for http url's, instead it should return false for http url's): -

const validateUrl = (url) => {
  return !!validUrl.isUri(url);
};

Thanks


Solution

  • How about extending your function with an additional line to check if it startsWith https//:

    const validateUrl = (url) => {
        const isValidUri = validUrl.isUri(url);
        return isValidUri && isValidUri.startsWith('https://');
    };