Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to validate URL if valid or invalid if the entry is like this:

www.com - should be invalid
http://ww.try.net - should be invalid
http://winner.com - should be valid
url = null = should be invalid



Below is the method I made which does not validate the above scenario.

Thank you in advance for your kind help

What I have tried:

C#


<pre> public bool IsUrlValid(string webUrl)
        {
            if (webUrl == null) return false;
            return Regex.IsMatch(webUrl, @"(http|https)://(([www\.])?|([\da-z-\.]+))\.([a-z\.]{2,3})$");
        }
Posted
Updated 17-Jan-17 22:47pm
Comments
Maciej Los 18-Jan-17 2:31am    
And what's wrong with your Regex pattern?

No matter of regex pattern, there's inbuilt function: Uri.IsWellFormedUriString Method (String, UriKind) (System)[^]

Exmaple:
C#
List<string> uris = new List<string>(){"www.com", "http://ww.try.net", "http://winner.com", "url = null"};

var result = uris.Select(x=> new {Uri = x, IsValid = Uri.IsWellFormedUriString(x, UriKind.Absolute)});

foreach(var u in result)
{
	Console.WriteLine("'{0}' is{1}valid", u.Uri, u.IsValid ? " ":" NOT ");
}

Result:
'www.com' is NOT valid
'http://ww.try.net' is valid
'http://winner.com' is valid
'url = null' is NOT valid


In case, you want to use Regex instead, you can use this: ^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$
 
Share this answer
 
v2
Comments
Peter Leow 18-Jan-17 2:42am    
That is handy, 5ed!
Maciej Los 18-Jan-17 2:47am    
Thank you, Peter.
Silver Lightning 18-Jan-17 4:32am    
Thank you sir. but
'http://ww.try.net' is Invalid (requirement)

because ww. should be www.
how can I adjust the regex to validate that the above URL is INVALID?
F-ES Sitecore 18-Jan-17 4:57am    
ww.try.net is a valid URL. How can you come up with a regex that reads peoples minds and knows that someone meant "www" and not "ww". If you are coming up with your own arbitrary rules that say what a valid URL is then you'll need to come up with a regex that satisfies what those rules are. We don't know what *you* consider to be a valid URL, only what the standards say is a valid url and ww.try.net is a valid URL.
Silver Lightning 18-Jan-17 5:21am    
Thanks sir. how about the URL link like this?
https://www.com
How could I make this invalid when the user input a URL like on the above case?
By itself, 'http://ww.try.net' is a valid url.
the 'www' is only the most common value used at this place, but any value is authorized, including no value.
 
Share this answer
 
Comments
Silver Lightning 18-Jan-17 4:02am    
thanks sir, but can you give me an updated regex code to set the http://ww.try.net' to become invalid?
try this:-

string pattern = @"/\b(http|https)\:\/\/(www\.)?[a-z]+\.[a-z]{2,3}\b/g";
// Instantiate the regular expression object.
Regex r = new Regex(pattern , RegexOptions.IgnoreCase);

// Match the regular expression pattern against a text string.
var m = r.Match(webUrl);
 
Share this answer
 
Comments
Silver Lightning 18-Jan-17 5:30am    
Thanks Singh
It's always resulting to invalid URL even the input URL is valid:

sample valid URL:
http://www.bigwinnerme.com

Could you elaborate more?
Singh Deepika 18-Jan-17 6:19am    
the regex is matching input string "http://www.bigwinnerme.com". Checkout at regexstorm.

What check are you applying. If the string wont match it will return Null. Also, you could test your regular expression here : http://regexstorm.net/tester

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900