How to check whether a string is a valid HTTP URL?
Try this to validate HTTP URLs (uriName
is the URI you want to test):
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& uriResult.Scheme == Uri.UriSchemeHttp;
Or, if you want to accept both HTTP and HTTPS URLs as valid (per J0e3gan's comment):
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
Check if a JavaScript string is a URL
A related question with an answer
Or this Regexp from Devshed:
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}
How to check whether a string is a valid url
I use this regex.
[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)
Here's the regex in action with this question's url:
So if a match is found you got yourself a URL. And if in the future you need to get parts of it it can do that for you too.
Dart / Flutter - Validating a string for URL
For some reason, the validators
package is requiring a pretty recent version of flutter's testing library rather than leaving it up to the application. Maybe there's a good reason for that (i.e. they're using a new feature).
The flutter engine internally requires a particular version of the flutter_test library (which is why it's generally a bad idea to specify a version of it). So to fix this you'll have to upgrade your flutter by running flutter upgrade
. If you're already at the most recent version of the channel you're in, you may have to run flutter channel dev
or flutter channel master
to switch to a channel (branch really) that's updated more often.
I run on the dev
branch/channel for the most part and while it very occasionally has problems, it doesn't happen a lot. I'd advise against using the master branch if possible though.
best URL validation
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.
Check if a string in C# is a URL
C# has Regex as well, but this seems simpler:
bool isUri = Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
(Answered at Regular expression for URL)
Related Topics
Easiest Way to Compare Arrays in C#
How to Set Custom JSONserializersettings for JSON.Net in ASP.NET Web API
Allow Windows Service to Interact with Desktop
Simplest Way to Do a Fire and Forget Method in C#
Globally Catch Exceptions in a Wpf Application
"The Remote Certificate Is Invalid According to the Validation Procedure." Using Gmail Smtp Server
Unity Game Manager. Script Works Only One Time
How to Write a Backslash (\) in a String
Cross-Thread Operation Not Valid
How to Clean SQLdependency from SQL Server Memory
Code for Decoding/Encoding a Modified Base64 Url (In ASP.NET Framework)
How to Use Global Variables in C#
Embedding JavaScript Engine into .Net
Differencebetween Nullable<T>.Hasvalue or Nullable<T> != Null