Windows forms text box should accept only url name
H开发者_开发知识库i I need to validate that a textbox should accept only url names. Can anybody tell me please. I would be really thankful.
I don´t think there is a built in method or class that you can use to validate a string as a legal url. But you can use regex, something like ["^a-zA-Z0-9-._"]+.([a-zA-Z][a-zA-Z]) If you use the code from Ranhiru you will not get it right with for instance bild.de and s.int which are both valid urls.
Do you need the URL to be valid or just of the right format? If it's the latter then you will probably need a regex as other answers point out - but getting it to work for all URLs could be tricky.
If it's the former then simply try and resolve the URL. There are several ways of doing this, each has drawbacks. For example, if you use "ping" then you'll need to remove any leading "http://" before hand.
However, this method isn't foolproof as a) you might not have an internet connection and b) the host might be down.
How about this:
public void Test()
{
Uri result = UrlIsValid("www.google.com");
if (result == null)
{
//Invalid Url format
}
else
{
if (UrlExists(result))
{
//Url is valid and exists
}
else
{
//Url is valid but the site doesn't exist
}
}
Console.ReadLine();
}
private static Uri UrlIsValid(string testUrl)
{
try
{
if (!(testUrl.StartsWith(@"http://") || testUrl.StartsWith(@"http://")))
{
testUrl = @"http://" + testUrl;
}
return new Uri(testUrl);
}
catch (UriFormatException)
{
return null;
}
}
private static bool UrlExists(Uri validUri)
{
try
{
WebRequest.Create(validUri).GetResponse();
return true;
}
catch (WebException)
{
return false;
}
}
If you only need to check that it's in the right format you can take out the UrlExists part.
You can use Regular Expressions to check whether the entered text is a valid URL :)
using System.Text.RegularExpressions;
private bool validateURL()
{
Regex urlCheck = new Regex("^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$");
if (urlCheck.IsMatch(txtUrlAddress.Text))
return true;
else
{
MessageBox.Show("The url address you have entered is incorrect!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
You can use this function as
if (validateURL() == true)
//Do something
else
//Do something else
Check for more RegularExpressions of URL's in Here http://www.regexlib.com/Search.aspx?k=url&c=-1&m=5&ps=20
Much simpler:
/// <summary>
/// Validates that the URL text can be parsed.
/// </summary>
/// <remarks>
/// Does not validate that it actually points to anything useful.
/// </remarks>
/// <param name="urlText">The URL text.</param>
/// <returns></returns>
private bool ValidateURL(string urlText)
{
bool result;
try
{
Uri check = new Uri(urlText);
result = true;
}
catch (UriFormatException)
{
result = false;
}
return result;
}
its working for me
private bool ValidateURL(string urlText) { bool result;
try
{
Uri check = new Uri(urlText);
result = true;
}
catch (UriFormatException)
{
result = false;
}
return result;
}
精彩评论