0

I want to validate url and its need to be allow to with and with out http://

that means if user type as http://www.google.com or www.google.com should allow to the user ..

i tried jquery validations class to do this.. no luck

i tried regx as follow its also not working as i want . any help very appreciated

<script>

var myVariable = "http://www.google.com/";
if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test('www.google.com/')) {
  alert("valid url");
} else {
  alert("invalid url");
}
</script> 
5
  • What do you call a "valid URL"? Is it a validly formatted string, or a URL that actually links to a resource? Commented May 31, 2011 at 4:32
  • hi i want google.com or wwww.google.com but not as www.google Commented May 31, 2011 at 4:34
  • The scheme-matching part seems incorrect - the way I read RFC 3986, a scheme must have at least 2 characters. RegExp seems to allow just one. A better RegExp might be: ^[a-zA-Z][a-zA-Z0-9\+\.-]+: Commented May 31, 2011 at 4:59
  • I'm curious if you're just focused on google. because if not, then you will have a problem with sites like youtu.be, fb.me, goo.gl which are valid... Commented May 31, 2011 at 5:14
  • 1
    www.google is a validly formated URI (assuming a scheme like HTTP and :// is prepended), the only way you can know it's not a real URI is to test it against a list of all valid URIs (a very long list) or send a request, presumbaly HTTP. That would most likely need to come from the server. So send the URI to the server and have it tested there. Commented May 31, 2011 at 5:15

4 Answers 4

4
<?php
$url = "http://www.example.com";

if(!filter_var($url, FILTER_VALIDATE_URL))
  {
  echo "URL is not valid";
  }
else
  {
  echo "URL is valid";
  }
?> 
Sign up to request clarification or add additional context in comments.

Comments

1

How about trying something like http://phpjs.org/functions/parse_url:485 which is based on Steve Levithan's well-tested, standards-aware parseUri function.

1 Comment

Or something like: ntfs://[email protected]:/what/the/home.dtb?thing=whatever&species=goose#beak
0
function checkURL(value) {
  var urlregex = new RegExp(
        "^((http|https|ftp)\://)*([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
  if(urlregex.test(value))
  {
    return(true);
  }
  return(false);
}

13 Comments

There are many schemes other than HTTP, HTTP and FTP (e.g. file). Host names don't have to start with www and URLs can include a port number (e.g. :80).
thank you but there is a problem when i type the www.google it also getting as valid url how could i avoid this
I have changed the regex. Now please check.
jsfiddle.net/Zqg7M/2 , i again updated the answer and fiddle. CHeck them for www.google.com
whoohooo... i guess this question should be raised as a community wiki... because there can be so many possiblities of a url.
|
0
<?php
// PHP 5.3.5-1ubuntu7.2
$url = "http://www.example.com:80i/"; // Yes, I have an "i" after port 80, then is not a valid URL

if (filter_var($url, FILTER_VALIDATE_URL)) {
  echo "URL is valid";
} else {
  echo "URL is NOT valid";
}

$url = parse_url($url);

echo $url['port']; // Returns 80 (without the "i")

// BUG...
?>

Using filter_var with FILTER_VALIDATE_URL doesn't validate correctly the URL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.