javascriptreactjsvalidation

Is there a way to validate and allow only https url's using .isUri i.e. without using Regex in JavaScript?


Is there a way to validate "https" url's using .isUri 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://');
    };