How can I check the url in javascript using regex

I want to check a text box that has some url value like blog address or website address how can I check this text box in js

+6
javascript url validation
source share
3 answers

I assume that you know how to get the <input> value with JavaScript. So your problem is to write a specific function to check the url.

First, we can consider checking that the URL is syntactically correct .

Some examples of what our URL validator should definitely consider OK:

 http://www.example.com. http://www.google.ee/search?q=foo&sourceid=opera&ie=utf-8&oe=utf-8 https://ama-z-on.co.uk/index.html?a=hei%20hoo ftp://ftp.linux.ee/public/gentoo/i386/portage-snapshot.tar.bz http://he.wikipedia.org/wiki/Χ©Χ˜Χ¨Χ’ΧœΧ™Χ™Χ#.D7.A8.D7.90.D7.95_.D7.92.D7.9D sftp://123.1.255.128:80/pub 

And this can only be a small fraction of the real variety of URLs. Both HTTP and FTP are not the only protocols available for URLs. Oh man, this checking URLs is really complicated.

But let's assume that a valid URL should start with a few letters, then β€œ: //” and after that, whatever. To test this pattern, you should use a regular expression, which in JavaScript looks like this:

 function validateUrl(url) { return /^[az]+:\/\//i.test(url); } 

Regular expressions are a whole big topic that you should learn yourself, but here is just a brief explanation:

  • / - start of regular expression
  • ^ - matches the beginning of a line
  • [az] - matches either a, b, c, ..., x, y, or z.
  • + - means that the previous pattern can be repeated one or more times.
  • : - matches the colon character itself.
  • \/ - matches forward-slash / (without the backslash, JavaScript would think that this is the end of the regular expression).
  • / - terminates the regular expression.
  • i is a modifier that makes this regular expression case insensitive.
  • .test(url) - calls the test method of the regular expression object with url as a parameter. When a parameter matches a regular expression, it returns true otherwise false .

Alternatively, you can allow the URL to be entered without the http:// - this means that you really need to check the domain name or IP address or something that follows it.

I guess you are quite confused, and that is intentional. You really don't have to write JavaScript to check the URLs yourself, it's too difficult to do it right. Instead, you should use a library function that has been tested and verified by many experts.

The JavaScript framework you are using may already have a good tool for this. In this case, use it. Unfortunately, I cannot offer a specific library for URL authentication.

Alternatively, you can consider ping URLs , as suggested by Josh Stodola, to verify that it really exists. Although the specific method suggested by Josh may be problematic if the resource referenced by the URL is a 10 GB file :)

+10
source share

Do a basic RegExp test for formatting. Then ping the URL using XMLHttpRequest to make sure it exists. JQuery example ...

 var url = $("#txtUserWebSite").val(); var reg = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+) ?@ )?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([,-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([,-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([,-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; if(!reg.test(url)) { alert("Invalid URL!"); return false; } $.get(url, function(dat, stat) { if(stat == "success") alert("Valid URL!"); else alert("Invalid URL!"); }) 
+2
source share

Check out the Regular Expression Library , which has a large repository of regular expressions for all kinds of validations. They also provide an online testing tool.

-one
source share

All Articles