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?

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.

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. :)

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");
?>
<?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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.