Return to the menu   Select another DES Module

Demonstrates the CustomValidator

When none of the other validators provide the desired evaluation rules, it's time to use the CustomValidator. A classic case involves querying your database against the value from the user, such as checking a login. It has no evaluation code of its own. You must write both client and server side validation code, although in some validators, the validation is strictly server side.

A CustomValidator does not need to know the IDs of fields to evaluate. Yet if you assign ControlIDToEvaluate and SecondControlIDToEvaluate, they are passed to your evaluation functions, allowing one function to be reused with different fields on the same page.

On the server side, attach the ServerCondition event handler to a method with this definition:

void MyMethod(PeterBlum.DES.IBaseCondition sourceCondition, PeterBlum.DES.IConditionEventArgs args)
Your function should set args.IsMatch to true if valid and false if invalid. If no evaluation was possible, set args.CannotEvaluate to true.

The args parameter holds values associated with ControlIDToEvaluate and SecondControlIDToEvaluate, but you must typecast args to PeterBlum.DES.Web.WebControls.ConditionTwoFieldEventArgs. It has these properties:

  • Value - The ControlIDToEvaluate control's value as a string. It has already been trimmed.
  • SecondValue - The SecondControlIDToEvaluate control's value as a string. It has already been trimmed.
  • ControlToEvaluate - Reference to the control assigned to ControlIDToEvaluate.
  • SecondControlToEvaluate - Reference to the control assigned to secondControlIDToEvaluate.

On the client-side, define a javascript function with this definition:

MyFunction(condition)
It returns one of 3 values: 1 for valid; 0 for invalid; and -1 for no evaluation was possible.

The parameter is the actual client-side object representation of the Condition object built into the validator. There are numerous ways to use it. Please refer to the Validation User's Guide for javascript functions that assist you.

Always assign the name of your function to the CustomEvalFunctionName property.


Controls

ControlIDToEvaluate is not used

You must enter the string "microsoft" for this to be valid.
Because ControlIDToEvaluate is not used, validation does not fire on the textbox's onchange event. It will fire when the page attempts to submit.



ControlIDToEvaluate is assigned to the TextBox

You must enter the string "microsoft" for this to be valid.



ControlIDToEvaluate and SecondControlIDEvaluate are assigned

You must not enter strings that begin with the same letter.
By default, validation does not occur until focus is off both textboxes. If you want validation to occur on each focus change, set ReportErrorsAfter to "EachEdit".





Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } } protected void Button2_Click(object sender, EventArgs e) { PeterBlum.DES.Globals.WebFormDirector.Validate(); if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } } protected void Example1(PeterBlum.DES.IBaseCondition sourceCondition, PeterBlum.DES.IConditionEventArgs args) { if (TextBox1.Text.Trim() == "microsoft") args.IsMatch = true; else args.IsMatch = false; } protected void Example2(PeterBlum.DES.IBaseCondition sourceCondition, PeterBlum.DES.IConditionEventArgs args) { string vText = ((PeterBlum.DES.Web.WebControls.ConditionTwoFieldEventArgs)args).Value; if (vText == "microsoft") args.IsMatch = true; else args.IsMatch = false; } protected void Example3(PeterBlum.DES.IBaseCondition sourceCondition, PeterBlum.DES.IConditionEventArgs args) { PeterBlum.DES.Web.WebControls.ConditionTwoFieldEventArgs twoFieldArgs = (PeterBlum.DES.Web.WebControls.ConditionTwoFieldEventArgs)args; string vText = twoFieldArgs.Value; // already trimmed string vText2 = twoFieldArgs.SecondValue; // already trimmed if ((vText.Length == 0) || (vText2.Length == 0)) args.CannotEvaluate = true; else if (vText[0] != vText2[0]) args.IsMatch = true; else args.IsMatch = false; }
</script> <script type="text/javascript">
function Example1(condition) { var vText = DES_GetById("<%= TextBox1.ClientID %>").value; if (vText.toLowerCase() == "microsoft") return 1; else return 0; } function Example2(condition) { // this function can get the string value of most controls, not just a textbox var vText = DES_GetTextValue(condition.IDToEval, condition.Trim); if (vText.toLowerCase() == "microsoft") return 1; else return 0; } function Example3(condition) { // this function can get the string value of most controls, not just a textbox var vText = DES_GetTextValue(condition.IDToEval, condition.Trim); var vText2 = DES_GetTextValue(condition.IDToEval2, condition.Trim); if ((vText.length == 0) || (vText2.length == 0)) return -1; // cannot evaluate without at least one character. This could also be return 1 if you feel this case is valid if (vText.toLowerCase().charAt(0) != vText2.toLowerCase().charAt(0)) return 1; else return 0; }
</script> <h2>ControlIDToEvaluate is not used</h2> You must enter the string "microsoft" for this to be valid.<br/> Because <span class="PropertyName">ControlIDToEvaluate</span> is not used, validation does not fire on the textbox's onchange event. It will fire when the page attempts to submit.<br/><br/> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <des:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Enter 'microsoft'" CustomEvalFunctionName="Example1" OnServerCondition="Example1" > </des:CustomValidator> <br/> <br/> <h2>ControlIDToEvaluate is assigned to the TextBox</h2> You must enter the string "microsoft" for this to be valid.<br/><br/> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <des:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Enter 'microsoft'" ControlIDToEvaluate="TextBox2" CustomEvalFunctionName="Example2" OnServerCondition="Example2" > </des:CustomValidator> <br/> <br/> <h2>ControlIDToEvaluate and SecondControlIDEvaluate are assigned</h2> You must not enter strings that begin with the same letter.<br/> By default, validation does not occur until focus is off both textboxes. If you want validation to occur on each focus change, set <span class="PropertyName">ReportErrorsAfter</span> to "EachEdit".<br/><br/> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <des:CustomValidator ID="CustomValidator3" runat="server" ErrorMessage="Text cannot start with the same letter" ControlIDToEvaluate="TextBox3" SecondControlIDToEvaluate="TextBox4" CustomEvalFunctionName="Example3" OnServerCondition="Example3" > </des:CustomValidator> <br/> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" ></des:Button><br/> <des:Button ID="Button2" runat="server" Text="Submit with only server side validation" OnClick="Button2_Click" CausesValidation="false"></des:Button>

Return to the menu   Select another DES Module