2014-04-07 102 views
0

我正在使用JavaScript網址驗證正則表達式。但它並不適用於所有工作辦案。希望下面的網址,就必須努力提供.com,。在等頁面沒有自動跳轉網址驗證不起作用

http://mysites.com 
    http://www.mysite.com 
    https://mysite.com 
    https://www.mysite.com 
    ftp://mysite.com 
    ftp://www.mysite.com 
    www.mysite.com 
    mysite.com 
    usa.mysite.com 

我使用正則表達式,

var regex = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; 

它不適用於www.mysite.com和mysite.com等。

任何人都請幫助我。

+0

你的反應是在這裏: http://stackoverflow.com/questions/1303872/trying-to-validate -url-using-javascript 請參閱:http://stackoverflow.com/questions/3653884/php-regex-for-url-validation-filter-var-is-too-permisive [1]:http: // STAC koverflow.com/questions/1303872/trying-to-validate-url-using-javascript –

回答

0

試試這個正則表達式:

var regex = /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/; 
+0

對不起,它不能正常工作。它不適用於「mysite.com」 –

0
<?php 
$url = "http://www.example.com"; 

if(!filter_var($url, FILTER_VALIDATE_URL)) 
{ 
    echo "URL is not valid"; 
} 
else 
{ 
    echo "URL is valid"; 
} 
?> 
0

試試這個:

"regex": /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uF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