开发者

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 m开发者_运维百科ake the validation. Otherwise leave it as it is. How it possible? Please help me by providing the regular expression for resolving this issue.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜