6

I am trying to validate URL in jQuery regular expression using below code. It is working fine with http://www and https://www

var myVariable = "http://www.example.com/2013/05/test-page-url-512-518.html";

if(/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www\.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(myVariable)){
    alert("valid url");
} else {
    alert("invalid url");
}

Edit:-

Above code work perfectly to validate URL. That time my requirement was only to validate http://www and https://www

Roopendra
  • 7,490
  • 16
  • 62
  • 87
  • http://stackoverflow.com/questions/2723140/validating-url-with-jquery-without-the-validate-plugin –  May 14 '13 at 07:45
  • 1
    Subdomains are the third level domains that are used to organize your web site content. !so http://wsww. is a valid url like https://mail.google.com... – Kld May 14 '13 at 07:49
  • @Khaled : thanks. Now I got. It means it is valid url... – Roopendra May 14 '13 at 07:51

5 Answers5

10

Check this - http://docs.jquery.com/Plugins/Validation/Methods/url

$("#myform").validate({
  rules: {
    field: {
      required: true,
      url: true
    }
  }
});
ChamingaD
  • 2,808
  • 8
  • 34
  • 57
  • 2
    This is better than the accepted answer. No need to write and verify the regex yourself + this doesn't only check for http:// at the beginning of the URL. – Ridz Jan 01 '14 at 17:48
2

You can change your Regex, actually there is a repetition of item i.e. http:\/\/|https:\/\/|www\.

This code will work out for you:

var myVariable = "http://www.example.com/2013/05/test-page-url-512-518.html";

if(/^(http:\/\/www\.|https:\/\/www\.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(myVariable)){
    alert("valid url");
} else {
    alert("invalid url");
}
Santosh Panda
  • 7,089
  • 8
  • 41
  • 54
2

You want to validate your URL:

Please pass the URL in this function then this will give you true OR false.

The Function :

<script>
function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}

isUrl(yourURL);
</script>
bummi
  • 26,839
  • 13
  • 60
  • 97
Vishnu Sharma
  • 1,299
  • 12
  • 11
1

your pattern is defined as

^(http:\/\/|someting else

thus any-urls begin with "http://" would be validated.

0

try this--->

var myVariable = "http://www.Test.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(myVariable)) {
  alert("valid url");
} else {
  alert("invalid url");
}
Ashok Damani
  • 3,852
  • 4
  • 27
  • 48