473,386 Members | 1,734 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,386 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 2725
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.