Validating URL using C# with Regular Expression

No.of Views4064
Bookmarked1 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : CSharp , How to
Validating URL using C# with Regular Expression
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Abstract

The most of the developer asking question inthe messageboard , how to valid URL using regex with C#.

The bigest advantage in regex we  can define a pattern in predfine way.then you could use the in the code.

Implementaion

{codecitation class="brush: c#; gutter: true;" width="600px"}

Private bool ValidateUrl(string url)

{

// regex pattern for url validation string

Regex RgxUrl = new Regex("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
if (RgxUrl.IsMatch(url))
{
return true;
}
else
{
return false;
}


}

{/codecitation}

The above code will validate URL's without http also. Alternatively you can use this way to validate url

{codecitation class="brush: c#; gutter: true;" width="600px"}

Private bool ValidateUrl(string url)

{
System.Globalization.CompareInfo cmpUrl = System.Globalization.CultureInfo.InvariantCulture.CompareInfo;
if(cmpUrl.IsPrefix(txtUrl.Text, "http://") == false)
{
txtUrl.Text = "http://" + txtUrl.Text;
}
Regex RgxUrl = new Regex("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
if (RgxUrl.IsMatch(url))
{
return true;
}
else
{
return false;

}

}

{/codecitation}


Incorporate in your project

Use this Funtion in your project click on the rihgt hand side top copy code icon the paste in your one the class file. Then pass the your url string value to this function, if valid string it will return true, otherwise  false.

Thank you

Amal.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
By:Another SolutionDate Of Posted:11/24/2011 11:30:49 AM
Dave
This is another solution... private bool ValidateUrl() { if (Uri.IsWellFormedUriString(txtServer.Text, UriKind.Absolute)) { return true; } else { return false; } }
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top