如何validationurl?
我想validationurl。 它应该接受:
http://google.com http://www.google.com www.google.com google.com
我推荐Regex来匹配URL 。
但它不支持google.com
。
只需在http://
如果它不存在然后进行测试。
if (inputURL.substring(0,7) != 'http://' && inputURL.substring(0,8) != 'https://') { inputURL = 'http://' + inputURL; }
不需要大型库或任何东西,只需几行代码。
function isUrl(s) { var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ return regexp.test(s); }
这是我认为应该用于validationurl的更好方法
reg = /https?:\/\/w{0,3}\w*?\.(\w*?\.)?\w{2,3}\S*|www\.(\w*?\.)?\w*?\.\w{2,3}\S*|(\w*?\.)?\w*?\.\w{2,3}[\/\?]\S*/ reg.test('www.google.com') # will return true reg.test('www.google') # will return false
如果你还没弄错,请告诉我。