Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to validate a URL in combobox.and i have tried; but i have one problem like when i type https://www.facebook then message come like valid Url.

but these is not correct . when i type then message should come as valid Url other wise it should display invalid Url

and the code i have tried is
using System.Text.RegularExpressions;

 public static bool isValidUrl(string url)
            string pattern = @"^(http|https|ftp|)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
            Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return reg.IsMatch(url);


private void comboBox_Url_Validating(object sender, CancelEventArgs e)
           bool s = isValidUrl(comboBox_Url.Text);
           if (s)
               MessageBox.Show("URL is valid.");
               MessageBox.Show("In valid URL.");
Updated 6-Aug-11 20:47pm

Here is the general expression


For your question, if you restrict http://www.facebook then what about You can even have http://localhost as valid address. So, I don't see anything wrong in your expression saying that url is valid. Still you can customize it your way.
Share this answer
UJimbo 4-Aug-11 7:45am    
I think what he's trying to say is that Regex finds http://www.facebook as a valid url, but it's not since it's missing the final .com part
Prerak Patel 4-Aug-11 7:47am    
But if he make it invalid, and http://localhost will also be invalid. And that is not true. This is my point.
UJimbo 4-Aug-11 7:49am    
I see what you mean
BobJanova 4-Aug-11 8:59am    
It is a valid URL, though. It doesn't find anything but you can set up local DNS so that it does.
rbnsubedi01 4-Aug-11 7:51am    
i want like http://www.facebook as a valid url, but it's not since it's missing the final .com part. so how can i valid that last part.
try the following:

Check If A Url Works in C#

And this:

C# URL Checker

Worked for me, I like the timeout part from the second link better
Share this answer
I recommend the following freeware to regular expression things
Expresso 3.0 - The premier regular expression development tool[^]
Share this answer
If you want to do a check if a url is valid the not so good way to do this is use the Uri class

Uri myUrl = new Url(someString);

This will break if the url is invalid. The Constructor of the Uri class is

if (uriString == null)
        throw new ArgumentNullException("uriString");
        this.CreateThis(uriString, false, UriKind.Absolute);

The CreateThis throws an exception if the uri is invalid.

If you like me and don't like the try catch way of working you can use

Uri myUrl = null;
            if (Uri.TryCreate(someString, UriKind.Absolute, out myUrl))
                //Valid Url
                //Invalid Url
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900