ASP.NET Custom Validator Validation not firing

I have been using ASP.NET CustomValidator when I want to validate field based on values of other fields.

For example, User must provide other contact info if selected.  For this let says we have other contact info check box and textbox to provide contact details.  If other contact info check box is selected, user must provide contact details.  So I need to check whether textbox value is empty or not only if check box is selected. I use asp.net custom validator to handle this kind of validation.

Here is the code that I used.

<script type="text/javascript">

        function ValidateDelimiterField(sender, args) {
            var chkTitle = document.getElementById("<%=chkLockDelimiter.ClientID%>");

            if (chkTitle.checked == true) {
                var value = args.Value;
                if (value == "") {
                    args.IsValid = false;
                    sender.innerText = "required";
                } else {
                    args.IsValid = true;
                }
                return;
            }
            args.IsValid = true;
            return;
        }

</script>

Other contact info:&nbsp;<asp:CheckBox runat="server" ID="chkLockDelimiter"/>

Contact Details:&nbsp;<asp:TextBox runat="server" ID="txtDelimiterText"></asp:TextBox>

<asp:CustomValidator runat="server" ID="cvDelimiter"
    ControlToValidate="txtDelimiterText"
    ErrorMessage="required"
    ClientValidationFunction="ValidateDelimiterField"
    Display="Dynamic"
    ForeColor="red" >
    </asp:CustomValidator>

<asp:Button runat="server" ID="btn" Text="Save" />

When you click on “save” button after selecting “Other contact info” check box, it should fire up custom validator and display message because input value is not provided for “contact details” text box but it’s not firing custom validator validation.  It’s not firing up client side function that specified for custom validator.  That sounds weird because it’s pretty simple.

I talked to couple friends at work and they told me that custom validator will only fire when the text box isn’t empty.  That was sounds strange for me.  When I looked at MSDN library for custom validator and I found property “ValidateEmptyText” of custom validator.  By specifing “ValidateEmptyText” property to true, it will fire up custom validator validation function even though text box is empty.

Hope this helps.  If this post really helps you, please click the Google +1 button to show it really helps you save your time.
Posted in Microsoft Technology Tagged with: ,
4 comments on “ASP.NET Custom Validator Validation not firing
  1. kk3d says:

    Thank you very much for this tip! It helped solve my problem.

  2. Cannon says:

    You got great points there, that’s why I always love checking out your blog.

    My blog:
    credit immobilier ou simulation de Rachat De Credit

  3. Moua12 says:

    I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again! “All that is gold does not glitter not all those that wander are lost.” by J. R. R. Tolkien.

  4. sonia says:

    Thanks very much!!! Great Post!!!

2 Pings/Trackbacks for "ASP.NET Custom Validator Validation not firing"
  1. ASP.NET Custom Validator Validation not firing | Broken Code…

    […]One Comment on “ ASP.NET Custom Validator Validation not firing ”. Howto: Fire an ASP.NET Validator from JavaScript | Broken Code January 28th, 2011 9:04 pm. […] […] Leave a Reply. Name. Email (will not be shared ). Website. Search …[…]…

Ads