如何验证网址? [英] How to validate url?
本文介绍了如何验证网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证网址。
它应接受:
I want to validate urls. It should accept:
http://google.com
http://www.google.com
www.google.com
google.com
我推荐使用 Regex来匹配网址。
但它不支持 google.com
。
推荐答案
如果它不在那里,只需添加 http://
然后测试。
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.
这篇关于如何验证网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文