views:

41

answers:

4

Hi I need to validate that a textbox should accept only url names. Can anybody tell me please. I would be really thankful.

A: 

You can use Regular Expressions to check whether the entered text is a valid URL :)

using System.Text.RegularExpressions;

private bool validateURL()
        {

            Regex urlCheck = new Regex("^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$");

            if (urlCheck.IsMatch(txtUrlAddress.Text))
                return true;
            else
            {
                MessageBox.Show("The url address you have entered is incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
                return false;

            }




        }

You can use this function as

   if (validateURL() == true)
          //Do something
    else
          //Do something else

Check for more RegularExpressions of URL's in Here http://www.regexlib.com/Search.aspx?k=url&c=-1&m=5&ps=20

Ranhiru Cooray
Firstly com, org, net, mil, and edu aren't the only valid domain name endings, and also the OP didn't say that it had to be strictly the root of a domain which your regex requires.
Davy8
@Davy8, This was a only a proposed solution. He can check and see if there is any other regular expression that he thinks is the best and use it. And i don't see any other way that you can check character by character whether it is a valid url :)
Ranhiru Cooray
+1  A: 

I don´t think there is a built in method or class that you can use to validate a string as a legal url. But you can use regex, something like ["^a-zA-Z0-9-._"]+.([a-zA-Z][a-zA-Z]) If you use the code from Ranhiru you will not get it right with for instance bild.de and s.int which are both valid urls.

bjorsig
Yes i agree that my regular expression would not work for all the cases. But using a different regular expression would work for him :)
Ranhiru Cooray
+1  A: 

Do you need the URL to be valid or just of the right format? If it's the latter then you will probably need a regex as other answers point out - but getting it to work for all URLs could be tricky.

If it's the former then simply try and resolve the URL. There are several ways of doing this, each has drawbacks. For example, if you use "ping" then you'll need to remove any leading "http://" before hand.

However, this method isn't foolproof as a) you might not have an internet connection and b) the host might be down.

ChrisF
+1  A: 

How about this:

    public void Test()
    {
        Uri result = UrlIsValid("www.google.com");
        if (result == null)
        {
            //Invalid Url format
        }
        else
        {
            if (UrlExists(result))
            {
                //Url is valid and exists
            }
            else
            {
                //Url is valid but the site doesn't exist
            }
        }
        Console.ReadLine();
    }

    private static Uri UrlIsValid(string testUrl)
    {
        try
        {
            if (!(testUrl.StartsWith(@"http://") || testUrl.StartsWith(@"http://")))
            {
                testUrl = @"http://" + testUrl;
            }
            return new Uri(testUrl);
        }
        catch (UriFormatException)
        {
            return null;
        }   
    }

    private static bool UrlExists(Uri validUri)
    {
        try
        {   
            WebRequest.Create(validUri).GetResponse();
            return true;
        }

        catch (WebException)
        {
            return false;
        }
    }

If you only need to check that it's in the right format you can take out the UrlExists part.

Davy8