best URL validation

im using code as below to check for the URL validation:

 public static bool CheckURLValid(string strURL)
  {
       Uri uriResult;
       return Uri.TryCreate(strURL, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
  }

The result as below should show all as true, but somehow it has its own pattern to validate the url:

false: google.com

true: http://www.google.com

false: https://www.google.com.my/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#newwindow=1&q=check%20if%20valid%20url%20c%23

true: https://stackoverflow.com/questions/ask

im using c#, how to enhance this checking url validation to be more accurate?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your CheckURLValid is returning exactly what you have told it to.

To return True on all 4 URLs here are the issues

false: google.com

This is a relative url and you have specified UriKind.Absolute which means this is false.

false: https://www.google.com.my/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#newwindow=1&q=check%20if%20valid%20url%20c%23

This is an httpS (Secure) url and your method says

&& uriResult.Scheme == Uri.UriSchemeHttp;

which will limit you to only http addresses (NON secure)

To get the results you are wanting you will need to use the following method:

public static bool CheckURLValid(string strURL)
{
    Uri uriResult;
    return Uri.TryCreate(strURL, UriKind.RelativeOrAbsolute, out uriResult);
}

An alternative is to just use
Uri.IsWellFormedUriString(strURL, UriKind.RelativeOrAbsolute);

and not re implement functionality that all ready exists. If you wanted to wrap it it your own CheckUrlValid I would use the following:
public static bool CheckURLValid(string strURL)
{
    return Uri.IsWellFormedUriString(strURL, UriKind.RelativeOrAbsolute); ;
}

The main problem is that most strings are valid relative URL’s so I would avoid using UriKind.RelativeOrAbsolute as google.com is an invalid url. Most web browsers silently add HTTP:// to the string to make it a valid url. HTTP://google.com is a valid url.

Method 2

Not sure if I’m missing something here, but just so others don’t waste their time with Uri.IsWellFormedUriString, note that the following test fails:

[TestMethod]
public void TestURLValidation()
{
    bool result = Uri.IsWellFormedUriString("bad", UriKind.RelativeOrAbsolute);
    Assert.IsFalse(result);
}

I.e., the prescribed answer will consider “bad” as a valid address. I believe that’s not the behavior most users are after.

Method 3

You can try

var isUrl = Uri.IsWellFormedUriString(strURL, UriKind.RelativeOrAbsolute);

It returns true on all four strings you wrote in your question.

Method 4

This is the best solution without using Regex:

(note that for example using only “IsWellFormedUriString” will return true for “//”)

    public static bool IsValidUrl(string url)
    {
        if (url == null)
        {
            return false;
        }

        try
        {
            Uri uriResult = new Uri(url);
            return Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
        }
        catch
        {
            return false;
        }
    }

For unit testing you can check the link where my function got nice results.

Method 5

I got it working by writing a small helper method that uses Regex to validate the url.

The following URL’s pass:

google.com

www.google.com

http://google.com

http://www.google.com

https://google.com/test/test

https://www.google.com/test

It fails on:

www.google.com/a bad path with white space/

Below is the helper method I created:

    public static bool ValidateUrl(string value, bool required, int minLength, int maxLength)
    {
        value = value.Trim();
        if (required == false && value == "") return true;
        if (required && value == "") return false;

        Regex pattern = new Regex(@"^(?:http(s)?://)?[w.-]+(?:.[w.-]+)+[w-._~:/?#[]@!$&'()*+,;=.]+$");
        Match match = pattern.Match(value);
        if (match.Success == false) return false;
        return true;
    }

This allows users to input any valid url, plus it accounts for bad url paths with white space which is exactly what I needed. I hope this helps someone


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x