2011-07-21 39 views
2

請指點如何驗證應該具有HTTP或HTTPS和www在它使用Javascript/jQuery的,驗證應具有URL HTTP或HTTPS和www

感謝 阿米特

+1

很抱歉,但你有沒有試過[搜索](http://www.google.com.ua/#source=hp&q=url+validation+javascript&oq = URL +驗證+ JA&A Q = 2&AQI = G2與AQL = gs_sm = E&gs_upl = 1648l5935l0l7190l9l8l0l2l2l0l153l657l2.4l6&計劃生育= 2c4748e2a65232a4&BIW = 1280&波黑= 888) –

+0

是的,我無法找到是檢查腳本HTTP和WWW – Amit

回答

4

嘗試使用一個url功能:

(function isURL(s) { 
    var regexp = /http(s?):\/\/www\.[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/; 
    return regexp.test(s); 
})("https://www.google.com") 

你可以發揮它here

+1

這失敗了'HTTP :// www.google.co.uk /'。我真的認爲你的正則表達式太廣泛了。 – pimvdb

0

試試這個:

^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])? 



var re = /^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/; 

alert(re.test(yourString)); 

Test it at this JsFiddle

Found at RegExLib

0

你可以使用:

function checkhttpWww(s) { 
    var reg = /http(s?):\/\/www/; 
     return reg.test(s); 
} 
0
function is_correct(x) { 
    return /^http[s]?:\/\/www\./.test(x); 
} 
相關問題