URL Validation using C#

Today, i will teach you how to create a program that will validate a URL inputted using C#. We know that 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 in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project validate url. 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 3. Import a namespace named System.Text.RegularExpressions because we will use regular expression function to commnunicate with our input. 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.
  1.     using System.Diagnostics;
  2.     using System;
  3.     using System.Windows.Forms;
  4.     using System.Collections;
  5.     using System.Drawing;
  6.     using System.Data;
  7.     using System.Collections.Generic;
  8.      
  9.     using System.Text.RegularExpressions;
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. We will have to initialize pattern as String to hold a string value of http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?. because this regular expression is the pattern for validating URL.
  1. string pattern = default(string);
  2. pattern = "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!. Note: It will be a valid URL if it has htpp at the start not www.
  1. if (Regex.IsMatch(TextBox1.Text, pattern))
  2. {
  3.         MessageBox.Show("Valid URL!");
  4. }
  5. else
  6. {
  7.         MessageBox.Show("Invalid URL!");
  8. }
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using Microsoft.VisualBasic;
  7. using System.Data;
  8. using System.Collections.Generic;
  9.  
  10.  
  11. using System.Text.RegularExpressions;
  12.  
  13. namespace validate_url
  14. {
  15.         public partial class Form1
  16.         {
  17.                 public Form1()
  18.                 {
  19.                         InitializeComponent();
  20.                        
  21.                
  22.                 }
  23.                
  24.                 public void Button1_Click(System.Object sender, System.EventArgs e)
  25.                 {
  26.                         string pattern = default(string);
  27.                         pattern = "http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\\'\\,]*)?";
  28.                         if (Regex.IsMatch(TextBox1.Text, pattern))
  29.                         {
  30.                                 MessageBox.Show("Valid URL!");
  31.                         }
  32.                         else
  33.                         {
  34.                                 MessageBox.Show("Invalid URL!");
  35.                         }
  36.                 }
  37.         }
  38. }
Output: output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment