Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. URL Validator

URL Validator

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    camasmartin
    wrote on last edited by
    #1

    :confused:Does anyone have a textbox based control that will only accept valid URLs? camasmartin hobby programmer

    H 1 Reply Last reply
    0
    • C camasmartin

      :confused:Does anyone have a textbox based control that will only accept valid URLs? camasmartin hobby programmer

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Handle the Validating even on a TextBox (or override OnValidating if deriving from TextBox) and in your validation code, use a Regex instance to check the URL, setting CancelEventArgs.Cancel to true if the regular expression fails. The regular expression you use is determined by what "URL"s you want to support. The concept of URLs - which is a form of URI - is very vague. Any protocol scheme is allows, though they might not match any protocol handlers. Common schemes are http(s), ftp(s), nntp, news, mailto, gopher, and telnet. Windows adds some (depending on what's installed) like ms-help, its, and some others. You can add them to Mozilla/Netscape, too. And each of these can have slightly different syntax that others don't support. My advice - unless you want several regular expressions that will slow validation tremendously, just use the Uri class like so:

      public class UrlBox : TextBox
      {
      // ...
      protected override void OnValidating(CancelEventArgs e)
      {
      try
      {
      new Uri(this.Text);
      }
      catch
      {
      e.Cancel = true;
      }
      }
      }

      This should handle most variants of URLs.

      Microsoft MVP, Visual C# My Articles

      C 1 Reply Last reply
      0
      • H Heath Stewart

        Handle the Validating even on a TextBox (or override OnValidating if deriving from TextBox) and in your validation code, use a Regex instance to check the URL, setting CancelEventArgs.Cancel to true if the regular expression fails. The regular expression you use is determined by what "URL"s you want to support. The concept of URLs - which is a form of URI - is very vague. Any protocol scheme is allows, though they might not match any protocol handlers. Common schemes are http(s), ftp(s), nntp, news, mailto, gopher, and telnet. Windows adds some (depending on what's installed) like ms-help, its, and some others. You can add them to Mozilla/Netscape, too. And each of these can have slightly different syntax that others don't support. My advice - unless you want several regular expressions that will slow validation tremendously, just use the Uri class like so:

        public class UrlBox : TextBox
        {
        // ...
        protected override void OnValidating(CancelEventArgs e)
        {
        try
        {
        new Uri(this.Text);
        }
        catch
        {
        e.Cancel = true;
        }
        }
        }

        This should handle most variants of URLs.

        Microsoft MVP, Visual C# My Articles

        C Offline
        C Offline
        camasmartin
        wrote on last edited by
        #3

        Thank you.:) With some tuning, it will come close enough. I didn't know about the Uri class. camasmartin hobby programmer Learning every day

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups