473,225 Members | 880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Checking if URL is valid

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
8 2719
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: - ions | last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie. M1,M2,M3.......throgh too M110 (thes are the messier objects, a catolouge of deep sky objects) the user selects of of these...
5
by: William Payne | last post by:
Hello, I am in the process of converting a C++ program to a C program. The user of the program is supposed to supply an integer on the command line and in the C++ version of the program I was using...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
2
by: Marlene Stebbins | last post by:
I am entering numbers into my program from the command line. I want to check whether they are > INT_MAX. Sounds simple, but I've discovered that if(x <= INT_MAX) { /* use x in some calculation...
2
by: JD | last post by:
Within asp.net I know it is possible to validate that a user has entered in a correctly formatted email address. However, what I want to be able to do is to verify that the email address entered is...
10
by: Fredrik Tolf | last post by:
If I have a variable which points to a function, can I check if certain argument list matches what the function wants before or when calling it? Currently, I'm trying to catch a TypeError when...
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
9
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r");...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
1
by: geetamadhavi | last post by:
Hi All, I have developed a php applciaiton where a new window is opening on checking the whether valid user orntot how to make that in same window after checking i have die(' not valid user ' ); i...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.