Saturday, 5 January 2013

Regular Expression to validate URL


ASP.NET  : Regular expression to validate URL.


Regex reg = new Regex(@"@"^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$");
Response.Write(reg.IsMatch(txtwebsite.Text));

string

regURL = @"^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$";




if (Regex.IsMatch(txtwebsite.Text, regURL))
{
// string match with regExpression.
}
Else
{
//String does not validate
}
 

No comments:

Post a Comment