|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWith ASP.NET, we were introduced to validators. In the most simplistic explanation, validators are used to verify that the information in a given control matches the values you are expecting. All ASP.NET validators are derived from the abstract
These validators are very well documented in the MSDN library, and I will not go into the details of these here. Creating the UriValidator classFor this article, we will walk through the process of creating a simple validator to verify that a value entered into a The process of accomplishing this is fairly basic. By deriving from the To do this, we will declare our class like this: public class UriValidator : System.Web.UI.WebControls.BaseValidator
{
protected override bool EvaluateIsValid()
{
TextBox textbox = FindControl(
this.ControlToValidate) as TextBox;
if (textbox != null)
{
try
{
Uri uri = new Uri(textbox.Text);
return true;
}
catch (UriFormatException)
{
return false;
}
}
return false;
}
}
What we are doing here is finding the Next, the protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(
this.ControlToValidate) as TextBox;
return ctrl != null;
}
Supporting selected Uri schemesAt this point, we have a fully functional validator, which will support any type of URI that is submitted. However, we may run across the case where we want to limit the scheme of the URI. To accomplish this, let's create a new The code will look something like this: [
Flags(),
Serializable()
]
public enum UriScheme
{
File = 0x0001,
Ftp = 0x0002,
Gopher = 0x0004,
Http = 0x0008,
Https = 0x0010,
Mailto = 0x0020,
News = 0x0040,
Nntp = 0x0080,
All = UriScheme.File | UriScheme.Ftp |
UriScheme.Gopher | UriScheme.Http | UriScheme.Https |
UriScheme.Mailto | UriScheme.News | UriScheme.Nntp
}
Notice that we have marked our Now that we have our Let's add this property accessor to our [
Category("Behavior"),
Description("Gets or sets a value indicating which Uri schemes will be accepted."),
DefaultValue("All")
]
public string AcceptedSchemes
{
get
{
object savedState = this.ViewState["AcceptedSchemes"];
if (savedState != null)
{
return ((UriScheme)savedState).ToString();
}
return UriScheme.All.ToString();
}
set
{
this.ViewState["AcceptedSchemes"] = (UriScheme)Enum.Parse(
typeof(UriScheme), value, false);
}
}
We have specified this property as a string so that the user can enter these into the designer property grid as a comma-delimited list of values (i.e.: We have also specified that this property lives in viewstate so that the value will be persisted across postbacks. Tying it all togetherNow that we have support for accepted schemes, we will need to modify the To do this, let's slightly modify the method and add a helper method to assist us in validating the scheme. protected override bool EvaluateIsValid()
{
TextBox textbox = FindControl(
this.ControlToValidate) as TextBox;
if (textbox != null)
{
try
{
Uri uri = new Uri(textbox.Text);
return IsValidScheme(uri.Scheme);
}
catch (UriFormatException)
{
return false;
}
}
return false;
}
private bool IsValidScheme(string scheme)
{
if (this.AcceptedSchemes.IndexOf("All") != -1)
{
return true;
}
if (scheme == Uri.UriSchemeFile)
{
return this.AcceptedSchemes.IndexOf("File") != -1;
}
else if (scheme == Uri.UriSchemeFtp)
{
return this.AcceptedSchemes.IndexOf("Ftp") != -1;
}
else if (scheme == Uri.UriSchemeGopher)
{
return this.AcceptedSchemes.IndexOf("Gopher") != -1;
}
else if (scheme == Uri.UriSchemeHttp)
{
return this.AcceptedSchemes.IndexOf("Http") != -1;
}
else if (scheme == Uri.UriSchemeHttps)
{
return this.AcceptedSchemes.IndexOf("Https") != -1;
}
else if (scheme == Uri.UriSchemeMailto)
{
return this.AcceptedSchemes.IndexOf("Mailto") != -1;
}
else if (scheme == Uri.UriSchemeNews)
{
return this.AcceptedSchemes.IndexOf("News") != -1;
}
else if (scheme == Uri.UriSchemeNntp)
{
return this.AcceptedSchemes.IndexOf("Nntp") != -1;
}
else
{
return false;
}
}
With this, we have completed our validator and are ready to drop this onto a webform to make sure everything works. Using the codeIn it's most simplest form, this control can be used like this: <html>
<body>
<form runat="server" ID="Form1">
<h3>UriValidator</h3>
<asp:TextBox id="textbox"
runat="server"></asp:TextBox>
<hw:UriValidator id="urlValidator"
runat="server" AcceptedSchemes="Http, Https"
ControlToValidate="textbox"
ErrorMessage="The Uri entered is an incorrect format."/>
<asp:Button id="button" runat="server"
Text="Submit"></asp:Button>
</form>
</body>
</html>
When you run this project, you can test that this is working by entering http://www.mattberther.com into the textbox. You will see that no validation errors are occurring. However, if you change the textbox value to Foo, you will notice that you get an error, because 'Foo' is not a valid URI. Now, one final test to validate that our scheme processing is working. Enter ftp://ftp.microsoft.com into the textbox, and click the button. Again, you will see that it fails, because our accepted schemes do not include FTP. DocumentationThis code has been documented using the Package ContentsIn the download, you will find the pre-built component, along with source and an NDoc generated .chm file that you can use for reference when using the component. FeedbackYour feedback is important to me and I am always willing to make improvements. If you found this article useful, don't forget to vote.
|
||||||||||||||||||||||||||||||||||||||||||||