Jasper
Jasper

Reputation: 5238

How to check if a variable contains a valid url using PHP and jQuery?

I'm trying to make a whois check script.

User can submit some domain address and then get a message if it's available or not.

$_POST['url'] is the submitted value by user.

How do I know if this variable is a domain name address?

It should give true for domains like:

http://google.com
www.google.com
http://www.google.com
google.com

Same for javascript (I'm using ajax-validation also)?

Upvotes: 0

Views: 702

Answers (2)

Lion King
Lion King

Reputation: 33813

You can use the following code:

Example:

$url = "http://0gate.com"; // you can use instead - $_POST['url']
if (!preg_match("/^[http|https]*[:\/\/]*[A-Za-z0-9\-_]+\.([A-Za-z]{3,4})+([\.A-Za-z]{3})*$/i", $url)) {
  echo "The domain [not valid - false]";
}else{
  echo "The domain is [valid - true]";
}

Upvotes: 1

slinzerthegod
slinzerthegod

Reputation: 625

If you want to check if the url is a valid url you could use filter_var() with the FILTER_VALIDATE_URL filter.

filter_var($_POST['url'], FILTER_VALIDATE_URL)

Upvotes: 6

Related Questions