How to validate url?

后端 未结 4 574
自闭症患者
自闭症患者 2020-12-21 06:11

I want to validate urls. It should accept:

http://google.com

http://www.google.com

www.google.com

google.com

I refer Regex to match URL

4条回答
  •  既然无缘
    2020-12-21 06:31

    Simply prepend http:// if it's not there and then test.

    if (inputURL.substring(0,7) != 'http://' && inputURL.substring(0,8) != 'https://') {
        inputURL = 'http://' + inputURL;
    }
    

    No large libraries required or anything, just a few lines of code.

提交回复
热议问题