Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp CSharp Validate Internet URL Function With Regular Expression

CSharp Validate Internet URL Function With Regular Expression

This is just a C# function that validate an internet URL to see whether it is a valid URL format. The function uses Regular Expression to validate input URL and an example in an ASP.net page will also be provided.

1. C# validate URL function

public bool validate_url(string internet_url)
{
    Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
 
    return reg.IsMatch(internet_url);
}

public bool validate_url(string internet_url) { Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"); return reg.IsMatch(internet_url); }

2. Put them all together with example in ASP.net page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
 
public partial class csharp_validate_url : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "http://4rapiddev.com";
 
        if (validate_url(url))
        {
            Response.Write(url + " is a VALID internet URL");
        }
        else
        {
            Response.Write(url + " is NOT a valid internet URL");
        }
        //Output: http://4rapiddev.com is a VALID internet URL 
    }
 
    public bool validate_url(string internet_url)
    {
        Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
 
        return reg.IsMatch(internet_url);
    }
}

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text.RegularExpressions; public partial class csharp_validate_url : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string url = "http://4rapiddev.com"; if (validate_url(url)) { Response.Write(url + " is a VALID internet URL"); } else { Response.Write(url + " is NOT a valid internet URL"); } //Output: http://4rapiddev.com is a VALID internet URL } public bool validate_url(string internet_url) { Regex reg = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"); return reg.IsMatch(internet_url); } }

Note: you need to declare System.Text.RegularExpressions namespace in order for Regex class works properly.

Download source code above.

Feb 5, 2012Hoan Huynh
Change Email Signature In Mozilla ThunderbirdC# Check Google PageRank - ASP.NET Example
You Might Also Like:
  • CSharp Validate Email Address Function With Regular Expression
  • Validate URL Use Regular Expression (Regex) In PHP
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • PHP validate ip address by using regular expression
  • Validate email using regular expression in ASP.NET
  • C# ASP.Net Validate Date
  • Auto Convert Text To URL Link And MailTo Email Address With ASP.NET C#
  • C# Generate Random Number Function
  • Simple Truncate Words Function In C#
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpRegex, regular expression, RegularExpressions306
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,459 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,839 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,655 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,994 views
C# Read Json From URL And Parse/Deserialize Json
11,722 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development