Check if a JavaScript string is a URL

前端 未结 30 3407
野趣味
野趣味 2020-11-22 15:41

Is there a way in JavaScript to check if a string is a URL?

RegExes are excluded because the URL is most likely written like stackoverflow; that is to s

30条回答
  •  温柔的废话
    2020-11-22 16:36

    To Validate Url using javascript is shown below

    function ValidURL(str) {
      var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
      if(!regex .test(str)) {
        alert("Please enter valid URL.");
        return false;
      } else {
        return true;
      }
    }
    

提交回复
热议问题