|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn an ideal world, web users don't need to authenticate because they are trustworthy, no try-catch because there wouldn't be any bug or unexpected behavior, and you wouldn't be using web validators because users will be submitting valid entries. But since we are not living in that world, I hope we would some time, you might find this validator useful, so bear with me. Developing with ASP.NET, I've encountered many cases when you only need one field to be filled out of many given fields. To validate in such cases, I used to have a custom validator, or a client-script, or some times did server-side validation only. The problem with a custom validator, and all other ASP.NET validators, is that they are mainly designed to handle one or two fields. Also, you need to write custom server-side and client-side code, or maybe copy and paste code, every time you encounter validating multiple fields. To solve this, I have made this light multiple-controls validator, and got use of the new ASP.NET 2.0 features. BackgroundEven with ASP.NET 2.0, a lot of valuable validation controls are missing, especially those for multiple-fields validation. Peter Blum has created a set of commercial controls called Professional Validation And More to fill this gap and he, when answering my forum post, gave me the idea of implementing my own validator. I have also found a good article on CodeProject by Daniel Hacquebord on CustomValidator dependent on multiple controls that addresses a similar need, however, this validation control has the following advantages:
There is another good article on CodeProject that gave me some inspiration, called RequiredIfValidator - Extending from the BaseValidator class about a validator that validates another field based on the selection of a drop down list. Using the Code
In the if(this.RenderUplevel) {
string clientID = this.ClientID;
Page.ClientScript.RegisterExpandoAttribute(clientID,
"evaluationfunction",
"MultipleFieldsValidatorEvaluateIsValid");
Page.ClientScript.RegisterExpandoAttribute(clientID,
"controlstovalidate",
this.GenerateClientSideControlsToValidate());
Page.ClientScript.RegisterExpandoAttribute(clientID,
"condition",
PropertyConverter.EnumToString(typeof(Conditions), Condition));
}
In the protected override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
if (base.RenderUplevel) {
this.Page.ClientScript.RegisterClientScriptResource(
typeof(MultipleFieldsValidator),
"AdamTibi.Web.UI.Validators.WebUIValidationExtended.js");
}
}
Using the ValidatorTo use this validator from your Visual Studio IDE, you need to add the provided .dll to the toolbox. For more information on this, check the MSDN documentation. After dropping the validator on a webform, all you need to do is to set the The <%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register TagPrefix="atv" Namespace="AdamTibi.Web.UI.Validators"
Assembly="AdamTibi.Web.UI.Validators" %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<atv:MultipleFieldsValidator ID="mfv" runat="server" Condition="OR"
ControlsToValidate="txtPhone,txtMobile,txtEmail">
fill at least one field</atv:MultipleFieldsValidator>
</form>
</body>
</html>
LimitationsI've only tested the validator on IE 6 and Firefox 1.5; however, I am not using any weird JavaScript so it should work on other browsers as well. Please let me know if it worked for you. I only need the validator to check ConclusionI hope I made someone’s day. If you like this article then please remember to vote. If you have any suggestions, bugs, or enhancements then hit me with it! History
|
||||||||||||||||||||||