locked
Custom Validator client-side validation RRS feed

  • Question

  • User-1430644438 posted

    I've been trying to get a custom validator component working that ensures at least one checkbox from a checkboxlist has been checked.  I've been used some code I found on dotnetjunkies.com but the client-side validation didn't work.  I've been attempting to modify it to get the client-side javascript validation to work with no luck.  Basically, I compile the below code into a DLL and add it to my bin folder.

    using System;
    using System.IO;
    using System.Text;
    using System.Reflection;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Global.WebControls
    {
        /// <summary>
        ///
    /// </summary> public class RequiredCheckBoxListValidator : CustomValidator { private ITextControl _controlToValidate; #region Overrides /// <summary> ///
    /// </summary> /// <returns>true</returns> //protected override bool ControlPropertiesValid() //{ return true; } protected override bool ControlPropertiesValid() { Control ctrl; ctrl = FindControl(ControlToValidate); if (ctrl != null) { _controlToValidate = (ITextControl)ctrl; return (_controlToValidate != null); } else { return false; } } /// <summary> ///
    /// </summary> /// <returns>true</returns> protected override bool EvaluateIsValid() { return this.EvaluateIsChecked(); } protected override void OnInit(EventArgs e) { if (this.DetermineRenderUplevel() && this.EnableClientScript) { this.ClientScript(); } base.OnInit(e); } /// <summary> /// Iterates through the ListControl's items looking for a selected item. /// </summary> /// <returns>True if any option is selected.</returns> protected bool EvaluateIsChecked() { ListControl _listcontrol = ((ListControl)this.FindControl(this.ControlToValidate)); foreach (ListItem li in _listcontrol.Items) {// So as long as 1 item is checked/selected, that's it. if (li.Selected) return true; } return false; } /// <summary> /// Construction of the Javascript validation code. /// </summary> protected void ClientScript() { //Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "RCBLV_Verify"); this.ClientValidationFunction = "RCBLV_Verify"; if (!this.Page.ClientScript.IsClientScriptBlockRegistered("RCBLVScript")) { //Assembly objAssembly = Assembly.GetExecutingAssembly(); //Stream objStream = objAssembly.GetManifestResourceStream("RCBLV.js"); //StreamReader objReader = new StreamReader(objStream); //string scriptBlock = objReader.ReadToEnd(); StringBuilder sb = new StringBuilder("<script language=\"javascript\">"); sb.AppendLine("<!--"); sb.AppendLine("function RCBLV_Verify(val)"); sb.AppendLine("{"); sb.AppendLine("var ctrl = document.getElementById(val.controltovalidate);"); sb.AppendLine("var col = ctrl.all;"); sb.AppendLine("for (i = 0; i < col.length; i++)"); sb.AppendLine("{"); sb.AppendLine("if (col[i].tagName.toUpperCase() == 'INPUT')"); sb.AppendLine("{"); sb.AppendLine("if (col[i].checked)"); sb.AppendLine("{ return true; }"); sb.AppendLine("}"); sb.AppendLine("}"); sb.AppendLine("return false;"); sb.AppendLine("}"); sb.AppendLine("//-->"); sb.AppendLine("</script>"); this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RCBLVScript", sb.ToString(), false); } } } }


    NEXT

    I add the following to an .aspx page.

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register TagPrefix="webcontrols" Namespace="Global.WebControls" Assembly="RequiredCheckBoxListValidator"%>
    
    <asp:CheckBoxList ID="_chkLivingSituation" runat="server">
    <asp:ListItem Value="1" Text="item 1" />
    <asp:ListItem Value="2" Text="item 2" />
    <asp:ListItem Value="3" Text="item 3" />
    </asp:CheckBoxList>
    <webcontrols:RequiredCheckBoxListValidator runat="Server" id="RequiredCheckBoxListValidator1"
    	ControlToValidate="_chkLivingSituation"
        Display="Dynamic"
        EnableClientScript="true"
        Text="* Required"
        />


    It validates on the server EVERY TIME.


    Wednesday, January 13, 2010 7:11 PM

Answers

  • User-1430644438 posted

    Turns out the javascript is junk and I overlooked it.

    StringBuilder sb = new StringBuilder("<script language=\"javascript\">");
                    sb.AppendLine("<!--");
                    sb.AppendLine("function RCBLV_Verify(sender, args)");
                    sb.AppendLine("{");
                    sb.AppendLine("var isChecked = false;");
                    sb.AppendLine("var chkboxlist = document.getElementById(sender.controltovalidate)");
                    sb.AppendLine("var arrayOfCheckBoxes = chkboxlist.getElementsByTagName(\"input\");");
                    sb.AppendLine("if (arrayOfCheckBoxes) {");
                    sb.AppendLine("if (typeof arrayOfCheckBoxes.length != 'undefined') {");
                    sb.AppendLine("for (i = 0; i < arrayOfCheckBoxes.length; i++) {");
                    sb.AppendLine("if (arrayOfCheckBoxes[i].checked) {");
                    sb.AppendLine("isChecked = true;");
                    sb.AppendLine("}");
                    sb.AppendLine("}");
                    sb.AppendLine("} else {");
                    sb.AppendLine("isChecked = chkboxlist.checked;");
                    sb.AppendLine("}");
                    sb.AppendLine("}");
                    sb.AppendLine("if(isChecked)");
                    sb.AppendLine("{args.IsValid = true;}");
                    sb.AppendLine("else {args.IsValid = false;}");
                    sb.AppendLine("}");
                    sb.AppendLine("//-->");
                    sb.AppendLine("</script>");



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2010 9:56 PM

All replies

  • User-1430644438 posted

    Turns out the javascript is junk and I overlooked it.

    StringBuilder sb = new StringBuilder("<script language=\"javascript\">");
                    sb.AppendLine("<!--");
                    sb.AppendLine("function RCBLV_Verify(sender, args)");
                    sb.AppendLine("{");
                    sb.AppendLine("var isChecked = false;");
                    sb.AppendLine("var chkboxlist = document.getElementById(sender.controltovalidate)");
                    sb.AppendLine("var arrayOfCheckBoxes = chkboxlist.getElementsByTagName(\"input\");");
                    sb.AppendLine("if (arrayOfCheckBoxes) {");
                    sb.AppendLine("if (typeof arrayOfCheckBoxes.length != 'undefined') {");
                    sb.AppendLine("for (i = 0; i < arrayOfCheckBoxes.length; i++) {");
                    sb.AppendLine("if (arrayOfCheckBoxes[i].checked) {");
                    sb.AppendLine("isChecked = true;");
                    sb.AppendLine("}");
                    sb.AppendLine("}");
                    sb.AppendLine("} else {");
                    sb.AppendLine("isChecked = chkboxlist.checked;");
                    sb.AppendLine("}");
                    sb.AppendLine("}");
                    sb.AppendLine("if(isChecked)");
                    sb.AppendLine("{args.IsValid = true;}");
                    sb.AppendLine("else {args.IsValid = false;}");
                    sb.AppendLine("}");
                    sb.AppendLine("//-->");
                    sb.AppendLine("</script>");



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2010 9:56 PM
  • User-1430644438 posted

    As a second part of the question... Does anyone know how to embed the javascript file to replace the StringBuilder.

    I was trying to do it in this commented out code but couldn't get it to work:

     //Assembly objAssembly = Assembly.GetExecutingAssembly();  
     //Stream objStream = objAssembly.GetManifestResourceStream("RCBLV.js");  
     //StreamReader objReader = new StreamReader(objStream);  
     //string scriptBlock = objReader.ReadToEnd();  






    Wednesday, January 13, 2010 10:03 PM