2013-10-12 93 views
-3

我想檢查輸入的url是否有效。如何檢查輸入的url是否有效?

這裏是我的代碼

var regExpURL = /((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; 
var websiteURL = $("#txtotherwebsite").val(); 
if(!regExpURL.test(websiteURL)) 
{ 
    $("#errorwebsite")[0].innerHTML = "Invalid website name."; 
    $("#txtotherwebsite").removeClass("successTextBox").addClass("errorTextBox"); 
    return false; 
} 
else 
{ 
    $("#errorwebsite")[0].innerHTML = ""; 
    $("#txtotherwebsite").removeClass("errorTextBox").addClass("successTextBox"); 
    return true; 
} 
return false; 

,但是當我用這它會在其他人只。 regExpURL.test(websiteURL)總是返回true。

+0

請給理由下投票 –

回答

-1

使用下面的正則表達式:

(?<Protocol>\w+):\/\/(?<Domain>[\[email protected]][\w.:@]+)\/?[\w\.?=%&=\[email protected]/$,]* 

爲了測試它去:http://regexlib.com/RETester.aspx

源文本認沽:

http://www.usgs.gov 
http://www.acl.lanl.gov/URI/archive/uri-archive.index.html 
ftp://@host.com/ 
ftp://host.com/ 
ftp://foo:@host.com/ 
ftp://[email protected]/%2Fetc/motd 
file://vms.host.edu/disk$user/my/notes/note12345.txt 
prospero://host.dom//pros/name 

放在正則表達式的文本框

(?<Protocol>\w+):\/\/(?<Domain>[\[email protected]][\w.:@]+)\/?[\w\.?=%&=\[email protected]/$,]* 

它將匹配所有上述URL的

希望它可以幫助你

+0

它的錯誤的正則表達式 –