RequiredSelection Validation Control


Hi guys, I was again thinking that lots of developers are stuck with the problem of selecting a minimum and maximum number of options from a CheckedListBox control and writing server side to validate this CheckedListBox control. So, please go ahead and use my new validation control RequiredSelection. This validation control will provide you the facility to need not to write single line of code. You can use it like other validation control. Let me know if you guys have any query. 

Before you build RequiredSelection control you need to perform the following steps:
  1. Create a class that inherits from BaseValidator class.
  2. Add MaxSelection and MiniSelection properties.
  3. Override the OnPreRender event and ControlPropertiesValid method.
  4. Generate required script to the validation
  5. Override the EvaluateIsValid() method.
Step 1:

Create a class that inherits from BaseValidator class (available in System.Web.UI.WebControls namespace).

public class RequiredSelection : BaseValidator

Step 2:

Add a MaxSelection property in the class (this is optional properties):

private Int32 _MaxSelection = -1;
public Int32 MaxSelection
{
    get { return _MaxSelection; }
    set
    {
        if (value <= 0)
        {
            throw new Exception("Maximum selection cannot be less than 1");
        }
        _MaxSelection = value;
    }
}

Add one property MinSelection in the class:

private Int32 _MinSelection = 1;
public Int32 MinSelection
{
    get { return _MinSelection; }
    set
    {
        if (value <= 0)
        {
            throw new Exception("Minimum selection cannot be less than 1");
        }
        _MinSelection = value;
    }
}

Step 3:

Override the OnPreRender event.

protected override void OnPreRender(EventArgs e)
{
    if (this.DetermineRenderUplevel() && this.EnableClientScript)
    {
        Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "CheckSelection");
        this.GenerateScript();
    }
    base.OnPreRender(e);
}

Override the ControlPropertiesValid method.

protected override bool ControlPropertiesValid()
{
    System.Web.UI.Control ctrl = null;
    ctrl = (CheckBoxList)this.FindControl(ControlToValidate);
    return (ctrl != null);
}

Step 4:

Generate required script to validate the MaxSelection and MinSelection properties.

private void GenerateScript()
{
    StringBuilder strScript = new StringBuilder();
    strScript.Append("");
    strScript.Append("<script language='javascript' type='text/javascript'>");
    strScript.Append("function CheckSelection(ctrl) {");
    strScript.Append("var listSelection = document.getElementById(document.getElementById(ctrl.id).controltovalidate);");
    strScript.Append("var listItems = listSelection.getElementsByTagName('input');");
    strScript.Append("var MinSelection = " + _MinSelection.ToString() + ";");
    strScript.Append("var MaxSelection = " + _MaxSelection.ToString() + ";");
    strScript.Append("var TotalSelection = 0;");
    strScript.Append("for (var i = 0; i < listItems.length; i++)");
    strScript.Append("{");
    strScript.Append("if (listItems[i].checked)");
    strScript.Append("{");
    strScript.Append("TotalSelection += 1;");
    strScript.Append("if (TotalSelection == MaxSelection) {");
    strScript.Append("return true;");
    strScript.Append("}");
    strScript.Append("}");
    strScript.Append("}");

    strScript.Append("if (TotalSelection < MinSelection) {");
    strScript.Append("return false;");
    strScript.Append("}");

    strScript.Append("if (TotalSelection < MaxSelection && MaxSelection != -1) {");
    strScript.Append("return false;");
    strScript.Append("}");

    strScript.Append("return true;");
    strScript.Append("}");

    strScript.Append("</script>");
    Page.ClientScript.RegisterClientScriptBlock(GetType(), "JSScript", strScript.ToString());
}

Step 5:

protected override bool EvaluateIsValid()
{
    return this.IsValid;
}

Up Next
    Ebook Download
    View all
    Learn
    View all