Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 .

But it is not supporting google.com .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
415 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...