0

I am trying to validate a URL. It should not contain a space in the beginning and in between. It should throw an error alert if we don't enter a complete URL. For example, if input is www.google, it should throw the following exception:

'Please enter a complete URL'

Can someone help me out with this?

Thank you

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
parshu123
  • 27
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [Validate email address in JavaScript?](https://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Rahul May 22 '17 at 14:42
  • 3
    Where is your code? What doesn't work? What do you expect? – Toto May 22 '17 at 14:42
  • What do you consider a complete url? – Mohit Bhardwaj May 22 '17 at 14:43
  • Why exactly is `www.google` incomplete? Because it's missing the protocol prefix (`http://`) or because `.google` is not an ICANN-approved TLD? – Álvaro González May 22 '17 at 14:44
  • @MohitBhardwaj a complete url is something like https://www.google.co.in, if i enter https://www.google. it should alert me with enter complete url Thanks – parshu123 May 22 '17 at 14:51
  • @ÁlvaroGonzález a complete url is something like google.co.in, if i enter google. it should alert me with enter complete url Thanks – parshu123 May 22 '17 at 14:51
  • A complete URL is something with three components? So `google.com` must be rejected? – Álvaro González May 22 '17 at 14:53
  • @ÁlvaroGonzález a complete url can be with four components like www.google.co.in – parshu123 May 22 '17 at 14:55
  • first you need to decide upon what logical criteria defines a valid url, because google.com, www.google.com, www.google.co.in all are valid. Perhaps you want that there must be `www` and after that there must be two or three strings separated by dot? – Mohit Bhardwaj May 22 '17 at 15:00
  • Out of curiosity, do you consider something random like `www.efg-cba.asdfgh` a valid domain or not? – Mi-Creativity May 22 '17 at 15:00
  • @MohitBhardwaj i want http or https in the first and then followed by www and then two or three components followed by it and it should not allow space in the begining and if it don't enter complete url it should alert me Thank You – parshu123 May 22 '17 at 15:03
  • ok, then let's say `http://www.efg-cba.asdfgh` is valid? even though it does not exist? – Mi-Creativity May 22 '17 at 15:03
  • Take a look at [**What is the best regular expression to check if a string is a valid URL?**](https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url). – cнŝdk May 22 '17 at 15:34
  • So actual sites that do not use a `www` prefix must be rejected? Why? – Álvaro González May 23 '17 at 07:18

3 Answers3

0

You can use the jQuery validate plugin to ensure valid URL's. Here's some example code taken from the website

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Makes "field" required and a url.</title>
<link rel="stylesheet" href="https://jqueryvalidation.org/files/demo/site-demos.css">



</head>
<body>
<form id="myform">
<label for="field">Required, URL: </label>
<input class="left" id="field" name="field">
<br/>
<input type="submit" value="Validate!">
</form>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      url: true
    }
  }
});
</script>
</body>
</html>
Garrett Kadillak
  • 1,026
  • 9
  • 18
0

You can use Regular Expressions to match the entered string with your pattern. Based on your specification of the url pattern, following example might do it:

var regex = /^(http)s?(:\/\/www)(\.\w{2,}){2,3}$/;

$("#check").on("click", function(e){
  e.preventDefault();
  var url = $("#url").val();
  if( regex.test(url) ){
    alert("URL is complete");
  }else{
    alert("Please enter a complete url");
  }
});//#check click()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input id="url" value="https://www.google.com" />
<button id="check">Validate</button>
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
0

I want http or https in the first and then followed by www and then two or three components followed by it and it should not allow space in the beginning.

REGEX: regex101 (1)

^https?:\/\/www\.(?:[a-z0-9]-?)+[^-\.]\.[a-z]{2,15}(?:\.[a-z]{2})?$
  • ^https? starts with http or https, hence the ? after the s letter.
  • :\/\/www\. matches ://www. (2)
  • (?: starts a non-capturing group.
  • [a-z0-9]-? matches the "name" part of the domain name, only letters and numbers with a possibility of one hyphen - after and not on the start.
  • )+[^-\.] closes the non-capturing group with the possibility of having more than one occurrence of [a-z0-9]-? to match URLs like http://www.some-domain-name.com, and it should not end with a dot . or a hyphen -, hence the [^-\.] part.
  • \. matches a dot .
  • [a-z]{2,15} is a set of 2 to 15, letters only, matches the gTLD [generic top-level domain] part of the domain name.
  • (?:\.[a-z]{2})?$ ends the regex with a non-capturing group of set of 2 letters only represents the ccTLD [country code top level domain] part, this part may exist or not this why we have ? after the group.

Code: jsFiddle 1

var domains = document.querySelectorAll('#domains-list li'),
  $URLregX = /^https?:\/\/www\.(?:[a-z0-9]-?)+[^-\.]\.[a-z]{2,15}(?:\.[a-z]{2})?$/i;

for (var i = 0, ln = domains.length; i < ln; i++) {
  var th = domains[i],
    urlText = th.innerHTML,
    $span = ($URLregX.test(urlText)) ? ' <span class="matched">Matched</span>' : ' <span class="not-matched">NOT Matched</span>';

  th.innerHTML = urlText + $span;
}
*{ padding:0; font-family:verdana, sans-serif; font-size:1em; }
li{ padding: 10px; border-bottom:1px dotted #AAA; }
li:nth-child(even){ background-color:#EEE; }
li span{ color:white; padding:3px 5px; border-radius:3px; font-size:12px; }
.matched{ background-color:green; }
.not-matched{ background-color:darkred; color:white; }
<ul id='domains-list'>
  <li>http://www.google.co.sa</li>
  <li>http://WWW.EXAMPLE.NET</li>
  <li>http://www.google.com</li>
  <li>http://www.some-site.engineering</li>
  <li>https://www.google.com</li>
  <li>http://www.efg-cba.asdfgh</li>
  <li>http://maps.google.com</li>
  <li>httphttps://www.example.net</li>
  <li>http://abc.site-name.com</li>
  <li>https://some-sub-domain.exam-ple.musem.uk</li>
  <li>http://google.com</li>
  <li>http://sub-domain.example-site.engineering</li>
  <li>http://test-www.mi-creativity.com</li>
  <li>https://google.co.in</li>
  <li>www.google</li>
  <li>www.google.com</li>
  <li>mail.google.com</li>
  <li>://www.google</li>
  <li>//www.google</li>
  <li>:google.com</li>
  <li>http://www..google.co.sa</li>
  <li>http://www.google..co.uk</li>
  <li>http://www.google.co..uk</li>
  <li>htt:google.com</li>
  <li>http:google.com</li>
  <li>http:/google.com</li>
  <li>http//www.google.com</li>
  <li>http://www.go ogle.com</li>
  <li>http://www.google.c om</li>
  <li>http://www.-google.com </li>
  <li>http://www.g--oogle.com </li>
  <li>http://www.google-.com</li>
  <li>http://www.-google.com</li>
  <li>http://www.g--oogle.com</li>
  <li>http://www.example.c</li>
</ul>

if I don't enter complete URL it should alert me

jsFiddle 2


Notes:

  1. This regex works for URL written in English only, there could be URLs or TLDs written in languages other than English, like: Arabic, Chinese, Indian, Japanese and Russian, the above regex will not match any of them. List of top-level domains.
  2. While sub-domains could be a combination of letters, numbers and a hyphen, the regex checks for www only because this is what the OP is asking for, to check for usual possibilities of the sub-domain part, this regex works for usual sub-domains as well as the case where there's no sub-domain like http://example.com.
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47