MAC
MAC

Reputation: 6577

Regular Expression for URL

I have one asp.net application, in which i have one text box for entering home page url. By default this text box contains http://. If the user entered additional text rather than the existing text, it will make the validation. Otherwise leave it as it is. How it possible? Please help me by providing the regular expression for resolving this issue.

Upvotes: 0

Views: 343

Answers (2)

Alex Nikolaenkov
Alex Nikolaenkov

Reputation: 2545

Validating URLs is not an easy task as it may seem. When writing such regular expressions you should keep in mind all the constraints. I've never seen an expression able to validate whether url is valid or not. F.i. there are URLs with local characters such http://президент.рф and it's not valid according to the regular experssion suggested by the MSDN. However it may be helpful to take a look at the Apache's UrlValidator

Upvotes: 1

Faheem
Faheem

Reputation: 3569

Try this expression

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$

From http://msdn.microsoft.com/en-us/library/ff650303.aspx#paght000001_commonregularexpressions

Or this mechanism in code behind

public bool IsValidUri(string uri)
{
  Uri validatedUri;
  return Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out validatedUri);
}

Posted by 0xA3 here Is there a URL validator on .Net?

HTH

Upvotes: 1

Related Questions