Check Valid or Invalid URL in VB.NET

A URL is the address of a specific Web site or file on the Internet. It cannot have spaces or certain other characters and uses forward slashes to denote different directories. Some examples of URLs are http://www.sourcecodester.com/, http://google.com/, etc. Now, let's start this validating a URL tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one TextBox named Textbox1 for inputting a desired URL. Add also one button named Button1 to know if the inputted URL is valid or not. You must design your layout like this: design 4. Now put add this code for your code module. The Button1_click here will trigger to decide if the inputted URL in the textbox is valid or not.
  1. Imports System.Text.RegularExpressions
  2. Public Class Form1
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim pattern As String
  5.         pattern = "http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"
  6.         If Regex.IsMatch(TextBox1.Text, pattern) Then
  7.             MsgBox("Valid URL!")
  8.         Else
  9.             MsgBox("Invalid URL!")
  10.         End If
  11.     End Sub
  12. End Class

Explanation:

We have imported System.Text.RegularExpressions because we will use a regular expression to match the pattern of the URL and the inputted URL in the textbox using IsMatch method. We have initialized pattern as String to hold a string value of http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?. If the pattern will match the inputted URL in the textbox then it will display Valid URL!. Otherwise, it will prompt an Invalid URL!.

Output:

A valid URL: output An Invalid URL: output Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below and hire me. Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Add new comment