Regular expression for validating URL in Javascript

I was looking for a decent regular expression to validate URLs that were entered as user input with.
After lots of searching on google i got the perfect solution which satisfy my requirement of my current assignment
Following is the regular expression for validating URL (Valid URL Regular Expression) a have used which worked perfectly.

^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

Following is the pattern results for the same
  • https://www.test.com   -Passed
  • http://www.test.com   -Passed
  • www.test.com   -Passed
  • test.com   -Passed
  • http://blog.test.com   -Passed
  • http://www.test.com/product   -Passed
  • http://www.test.com/products?id=1&page=2   -Passed
  • http://www.test.com#up   -Passed
  • http://255.255.255.255   -Passed
  • 255.255.255.255   -Passed
  • http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2   -Failed
  • http://www.site.com:8008   -Passed

Comment if you like it!!

Comments

Popular posts from this blog

Restore the lost focus of Auto post back controls in asp.net update Panel control

Disable backspace key using Jquery

Clone a generic list in C# using extension method

Step by step migration of ASP.NET Core 2.2 to 3.1

Add Comma automatically while entering amounts in textbox using Javascript

Remove Owin from MVC 5 Application and use asp.net custom forms authentication

Source Code Beautifier And Format Source Code For Blogger and Websites

Email validation using regular expression in jQuery

Send bulk Email in asp.net using excel file