Results 1 to 8 of 8

Thread: [RESOLVED] [2.0] how to validate uri?

  1. #1

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Resolved [RESOLVED] [2.0] how to validate uri?

    how do i check if a uri is valid and contains "http://" at the start of it?
    eg: "www.google.com" is invalid but "http://www.google.com" is valid.

    is there some class method in .net to check this or do i need to use regex?
    (i have looked at the online msdn, but might have missed something...)
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [2.0] how to validate uri?

    Use either string.StartsWith() or string.SubString()
    VB Code:
    1. string s = "http://w3c.org";
    2.  
    3. //method 1
    4. if(s.StartsWith("http://"))
    5.     MessageBox.Show("URI has http://");
    6. else
    7.     MessageBox.Show("URI don't have http://");
    8.  
    9. //method 2
    10. s = "http//www.google.org";
    11. if(s.Substring(0,7)=="http://")
    12.     MessageBox.Show("URI has http://");
    13. else
    14.     MessageBox.Show("URI don't have http://");
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [2.0] how to validate uri?

    great! thanks for the help
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] how to validate uri?

    Also have a look in the System.Uri namespace.

  5. #5

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [2.0] how to validate uri?

    Quote Originally Posted by penagate
    Also have a look in the System.Uri namespace.
    Thanks for mentioning that again...

    Trying to create a new instance of System.Uri without the 'http://' in the address results in an UriFormatException

    EDIT: System.Uri is a class, not a namespace
    Last edited by tr333; Jan 8th, 2007 at 12:05 AM.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] how to validate uri?

    Eh... yes. My mistake.

  7. #7

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [2.0] how to validate uri?

    for anyone else that might stumble upon this thread, here is some sample code of my solution to the problem:

    Code:
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(isValidUri(@"www.google.com").ToString());
            Console.WriteLine(isValidUri(@"http://www.google.com").ToString());
            Console.ReadLine();
        }
    
        public bool isValidUri(string uri)
        {
            try
            {
                Uri address = new Uri(uri);
                return true;
            }
            catch (UriFormatException ex)
            {
                return false;
            }
        }
    }
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  8. #8
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: [RESOLVED] [2.0] how to validate uri?

    A regular expression would save you the trouble of excessive overhead involved in exceptions:
    http://regexlib.com/Search.aspx?k=url
    http://regexlib.com/REDetails.aspx?regexp_id=1363

    Code:
    // RegEx is found at System.Text.RegularExpressions.
    
    		/// <summary>
    		/// <para>Tests if the provided URL is valid.</para>
    		/// <para>Supports address, IP address, with or without the protocol, optional port numbers, directory path,
    		/// file name (with extension), and parameter pairs.</para>
    		/// </summary>
    		/// <param name="url">The URL to test.</param>
    		/// <returns>True if the URL is valid, else false.</returns>
    		private bool IsValidUrl(string url)
    		{
    			const string URL_REGEX = @"(?#WebOrIP)((?#protocol)((http|https):\/\/)?(?#subDomain)(([a-zA-Z0-9]+\.(?#domain)[a-zA-Z0-9\-]+(?#TLD)(\.[a-zA-Z]+){1,2})|(?#IPAddress)((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])))+(?#Port)(:[1-9][0-9]*)?)+(?#Path)((\/((?#dirOrFileName)[a-zA-Z0-9_\-\%\~\+]+)?)*)?(?#extension)(\.([a-zA-Z0-9_]+))?(?#parameters)(\?([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?(?#additionalParameters)(\&([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?)*)?";
    			Regex urlTest = new Regex(URL_REGEX, RegexOptions.IgnoreCase | RegexOptions.Compiled);
    			return urlTest.IsMatch(url);
    		}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width