Reputation: 1108
i made that:
/^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}
and checked it with a validator but on my page it is not working:
var re = /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1};
if (!re.test(url)) {
alert("url error");
return false;
}
i get this error
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Timestamp: Tue, 30 Nov 2010 14:23:10 UTC
Message: Expected ')' in regular expression
Line: 781
Char: 23
Code: 0
URI: http://*************************
Upvotes: 13
Views: 51720
Reputation: 145
var regForUrl = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if (!regForUrl.test(url)) {
alert('Invalid URL-- missing "http://" or "https://"');
}
Upvotes: 0
Reputation: 2283
After a long research I build this reg expression. I hope it will help others too.......
url = 'https://google.co.in';
var re = /[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/;
if (!re.test(url)) {
alert("url error");
return false;
}else{
alert('success')
}
Upvotes: 0
Reputation: 8586
Just in case you want to know if the url really exists:
function url_exist($url){//se passar a URL existe
$c=curl_init();
curl_setopt($c,CURLOPT_URL,$url);
curl_setopt($c,CURLOPT_HEADER,1);//get the header
curl_setopt($c,CURLOPT_NOBODY,1);//and *only* get the header
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it
curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url
if(!curl_exec($c)){
//echo $url.' inexists';
return false;
}else{
//echo $url.' exists';
return true;
}
//$httpcode=curl_getinfo($c,CURLINFO_HTTP_CODE);
//return ($httpcode<400);
}
Upvotes: 1
Reputation: 34652
I will post, although the question has been accepted.
That regex is still incomplete.
http://www.-1-.de
is not a valid domain name but would pass your test.
Here's what I use:
~^
(?:ht|f)tps?://
(?:[a-z0-9] (?:[a-z0-9-]*[a-z0-9])? \.)*
(?:[a-z0-9][a-z0-9-]{0,62}[a-z0-9])
(?:\.[a-z]{2,5}){1,2}
$~ix
Covers http(s), ftp(s) and .co.uk TLDs and the like. Also covers subdomains which can be 1 character in length (m.example.com
for mobile versions of webpages) but will not allow m-.example.com
.
Surely some might object as to the regex's completeness, since .pro
TLDs require at least 4 characters as a domain name. ;-)
Also IDN domain names will only pass my regex after conversion (i.e. in the "xn--" format).
Upvotes: 2
Reputation: 91528
As said Nick you have to escape \
and .
unless you're using another delimitor so your regex will become :
var re = '~(https?)://)?(www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}\.?~';
But be aware that your regex will match url like:
http://....aa.
Upvotes: 0
Reputation: 630597
You have to escape your special characters (/
and that .
after www
in this case) and att the missing trailing /
, like this:
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) {
alert("url error");
return false;
}
Upvotes: 35