Monday, May 26, 2014

How to valid URL in ASP.NET MVC data annotation?


[Display("URL")]
[RegularExpression(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$", ErrorMessage = "Invalid URL")]
public string Url { get; set; }


Reference:
Website to test regular expression
http://regex101.com/

Common Regular Expressions
http://msdn.microsoft.com/en-us/library/ff650303.aspx