1,105,578 Community Members

URL Validation

Member Avatar
fernandodonster
Newbie Poster
12 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How do we validate URL either with PHP/javascript? could u help me?for example,
whether input text is in the format http://www.google.com or not?

Member Avatar
php_daemon
Junior Poster
138 posts since Aug 2006
Reputation Points: 3 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 
if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
  echo "wrong url";
} else {
  echo "ok";
}
Member Avatar
paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 1 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 
if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
  echo "wrong url";
} else {
  echo "ok";
}

that looks good, except that only letters, numbers, and hypens are allowed in a domain name (underscores are not allowed), and due to ICANN limitation domain names are limited to 64 characters. This regular expression also limits a lot of cases, for instance none of the following special cases would work...

any website with a subdomain/or lack of one other than www, for instance
http://slashdot.org/ (has no www and no subdomain)
http://en.wikipedia.org/ (has a subdomain en, but no www)

subfolders
http://www.mozilla.com/en-US/firefox/

ip addresses...
http://64.233.167.99/
you will actually get google.com (that is its ip address as of this current moment).

specifying port numbers:
http://glcfapp.umiacs.umd.edu:8080

entering username/passwords directly into the url
http://username: password@example.com

i recommend going here: http://regexlib.com/Search.aspx
and performing a search for category "Uri" and see which regular expression you thing would work best for this example. here is a sample i got from that site, of course this one is not perfect either, but it's another sample.

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

just know that it will probably be virtually impossible to find a perfect regex expression to match all possible URL's, one way would be to see if it starts with http:// and if it does, then see if you can make a connection to it with php, if the headers return a status code of 200, then you are good and you know it's a valid url, but that is a lot of work in itself.

Member Avatar
php_daemon
Junior Poster
138 posts since Aug 2006
Reputation Points: 3 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes, indeed. Though if the OP needs to check for the urls of specific format only, such as the one provided as an example, a simple regexp would do quite well. :)

Member Avatar
ym_chaitu
Newbie Poster
18 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

check this out

<?php
$subscr=$_REQUEST['subscr'];

//url validation

if (preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $subscr)) 
{
    print "$subscr url OK.";
} else {
    print "$subscr url not valid!";
}
//echo("$subscr");
?>
Member Avatar
Somnathpawar
Newbie Poster
2 posts since Sep 2010
Reputation Points: 5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
<?php
$websiteUrl = $_REQUEST['url'];

if (!preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$websiteUrl)) {
echo "wrong url";
} else {
echo "ok";
}

?>

Check this one. This is perfect.

Member Avatar
prof php
Newbie Poster
13 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Gotta complement, this works like magic. tried it out and it's fabulous.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article