Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
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 https://www.facebook.com then message should come as valid Url other wise it should display invalid Url

and the code i have tried is
C#
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);
        }

 



C#
private void comboBox_Url_Validating(object sender, CancelEventArgs e)
       {
           bool s = isValidUrl(comboBox_Url.Text);
           if (s)
           {
               MessageBox.Show("URL is valid.");
              
           }
           else
           {
               MessageBox.Show("In valid URL.");
               this.comboBox_Url.Focus();
           }
Posted
Updated 6-Aug-11 20:47pm
v4

Here is the general expression
^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$

Ref:http://msdn.microsoft.com/en-us/library/ff650303.aspx[^]

For your question, if you restrict http://www.facebook then what about http://google.com. 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
 
Comments
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, http://google.com 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
 
v2
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

C#
Uri myUrl = new Url(someString);


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

C#
if (uriString == null)
        throw new ArgumentNullException("uriString");
      else
        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

C#
Uri myUrl = null;
            if (Uri.TryCreate(someString, UriKind.Absolute, out myUrl))
            {
                //Valid Url
            }
            else
            {
                //Invalid Url
            }
 
Share this answer
 
v2

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