By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

Checking if URL is valid

P: n/a
What kind of regular expression pattern is needed to check
if URL is valid? It's enought if most of cases are covered.

I have PHP 4.x.

Br
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
John V wrote:
What kind of regular expression pattern is needed to check
if URL is valid? It's enought if most of cases are covered.

I have PHP 4.x.

Br


Personally, I check if the last part of the domain name (the .com, .net,
..info) part is greater than one character (to allow for country specific
domain names like ca or uk) and less than 8 characters (because I
believe there are also .museum or similar available on the net, andmore
will eventually follow).

I should (but don't) check if the last part of the domain name is
numeric (thus instead of .com that the user had entered .co1 or something).

I do check that there are at least three 'portions' to the domain name -
thus http://www.com would fail but www.xyz.com would pass - Or xyz.co.uk
would also pass...

And what about the protocol? Are you only refering to web http://
style addresses? If true, then check for that too... I believe there is
a maximum length for a domain name but I don't know what that is - I
have a self imposed limit of 64characters.

Lastly - your url should I believe only contain alphanumeric input, in
addition to _ (underscore) characters (though I believe you could also
have a dash, but I think this is not recommended).

Does that help you any?
randelld

Jul 17 '05 #2

P: n/a
Randell D. <re******************************@fiprojects.moc > wrote:
What kind of regular expression pattern is needed to check
if URL is valid? It's enought if most of cases are covered.

You can't check it this way. Why not check if it actually exists?
Personally, I check if the last part of the domain name (the .com, .net,
.info) part is greater than one character (to allow for country specific
domain names like ca or uk) and less than 8 characters (because I
believe there are also .museum or similar available on the net, andmore
will eventually follow).
fo*@bar.invalid is a valid URL accoring to RFC 2606 but email will never
be delivered to it.
I do check that there are at least three 'portions' to the domain name -
thus http://www.com would fail but www.xyz.com would pass - Or xyz.co.uk
would also pass...
Funny example:

lynx -head -dump http://www.com/

HTTP/1.0 200 OK
Server: Resin/2.1.14
ETag: "AAAAQH7CT4Q"
Last-Modified: Thu, 10 Feb 2005 06:51:22 GMT
Content-Type: text/html
Content-Length: 9538
Date: Sun, 20 Feb 2005 03:23:06 GMT

A valid URL it seems
And what about the protocol? Are you only refering to web http://
style addresses? If true, then check for that too... I believe there is
a maximum length for a domain name but I don't know what that is - I
have a self imposed limit of 64characters.

Lastly - your url should I believe only contain alphanumeric input, in
addition to _ (underscore) characters (though I believe you could also
have a dash, but I think this is not recommended).


Heard of IDN?

So while above test might be a good start (and be better than no checks)
the best test is to actually check if the URL is valid. Even better
would be a kind of challenge/response test (a must for email URLs).

Jul 17 '05 #3

P: n/a
On Sun, 20 Feb 2005 00:56:31 GMT, "Randell D."
<re******************************@fiprojects.moc > wrote:
I do check that there are at least three 'portions' to the domain name -
thus http://www.com would fail but www.xyz.com would pass - Or xyz.co.uk
would also pass...


http://xyz.com is a valid URL.

--
Regards, Paul Herber, Sandrila Ltd. http://www.pherber.com/
SanDriLa - SDL/MSC/TTCN/UML2 application for Visio http://www.sandrila.co.uk/
email address is spam-trapped - s/*@/paul@/
Jul 17 '05 #4

P: n/a
Daniel Tryba wrote:
fo*@bar.invalid is a valid URL accoring to RFC 2606 but email will
never be delivered to it.


Funny - I was under the impression that email is never delivered to
URLs :)

--
Markku Uttula

Jul 17 '05 #5

P: n/a
Randell D. wrote:
Personally, I check if the last part of the domain name (the .com,
.net, .info) part is greater than one character (to allow for
country
specific domain names like ca or uk) and less than 8 characters
(because I believe there are also .museum or similar available on
the net,
andmore will eventually follow).
And thus you negate the possibility of passing in numeric URLs. Never
heard of IP names?
I do check that there are at least three 'portions' to the domain
name - thus http://www.com would fail but www.xyz.com would pass -
Or
xyz.co.uk would also pass...
Why? As has been pointed out, www.com exists and is fully valid URL.
There are others too.
And what about the protocol? Are you only refering to web http://
style addresses? If true, then check for that too... I believe
there
is a maximum length for a domain name but I don't know what that
is - I
have a self imposed limit of 64characters.
What matter does that have? The question was about URL, and in that
domain name length plays a very little part.
Lastly - your url should I believe only contain alphanumeric input,
in
addition to _ (underscore) characters (though I believe you could
also
have a dash, but I think this is not recommended).


In addition, there can be a lot more characters, because we're talking
about a URL, not "domain name part" of URL.

--
Markku Uttula

Jul 17 '05 #6

P: n/a
John V wrote:
What kind of regular expression pattern is needed to check
if URL is valid? It's enought if most of cases are covered.


I wouldn't recommend using regular expression to do that. Why do it
the hard way :)

A much better solution is to attempt to connect to the given domain,
and if that is possible, request (for example) HEAD-command with the
URL entered. This would (of course) only check if the connection to
the given URL is possible at the given time from the machine you're
running the script from, but this is always the case with WWW :)

--
Markku Uttula

Jul 17 '05 #7

P: n/a
Markku Uttula <ma***********@disconova.com> wrote:
fo*@bar.invalid is a valid URL accoring to RFC 2606 but email will
never be delivered to it.


Funny - I was under the impression that email is never delivered to
URLs :)


A common mistake, worth reading:

mailto URL scheme:
http://www.ietf.org/rfc/rfc2368.txt
Generic URI syntax:
http://www.ietf.org/rfc/rfc2396.txt

But I see I forgot the scheme from my example url :)
Jul 17 '05 #8

P: n/a
Randell D. wrote:
John V wrote:

[snip]
It's enought if most of cases are covered.

[snip]

I never said my solution covered everything - nor did the OP request
this... but I do think my suggestion covered most cases...

Thanks for pointing out things I hadn't thought off though,

randelld
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.