Validation of URLs

User Nawal Omar has sent us the following question:

…is there is a similar function in survey solution for URL validation I found out recently they introduced this : self.IsValidEmail() but it only works for email validation

One can start with the standard C# method Uri.IsWellFormedUriString() as documented here:

But this may be too generic for most common uses and may require subsequent restriction to a particular scheme, such as illustrated in the “PUBLIC EXAMPLE Unicode Text and Formatting”

Mind also the difference between the URLs and URIs.

Example of an accepted URI:


Example of an URI considered by this function as invalid:

(even though the browser accepts the specified string).

Example of an URI, which is valid, but not accepted since it is not following allowed scheme:


It may take quite a bit of fine-tuning to narrow down to a particular range of addresses that you wish to treat as “valid” while treating the rest as invalid. If the C# standard function is not giving sufficient flexibility, consider using regular expressions, which should give you full flexibility on formulating what to consider desirable. There are plenty of examples in the internet, such as, for example, this 3rd party website:

new Regex("^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$")


new Regex("^[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$")

Hope this helps, Sergiy