Howto: Fire an ASP.NET Validator from JavaScript

Let’s take an example of my previous post and make it little bit user friendly.  Here is the demo page link to see in live action.

In the example(#1. Without ValidatorEnable), Custom validator fires and shows a validation message only when you click on save button.  To make it user friendly, UI should display message when user select “Other Contact info” checkbox.  It will be very frustrating when you have lengthy form and it pops up all validation messages when user submit the form.

We should fire custom validator when user select “Other Contact Info” checkbox and display validation message if validator fails.  That can be achieved by calling client side validation function that you specified for custom validator and that works fine but what happen if user deselect the checkbox.  That validation message will stay there unless you clear that message in JavaScript.

The easy way to handle this situation, there is a method called “ValidatorEnable” that does the same thing what we need rathar than doing it ourself.

ValidatorEnable(clientIdOfCustomValidator,true/false);

When you call ValidatorEnable(clientIdOfCustomValidator,true), it will enable the client validator and fire up client validator function.

When you call ValidatorEnable(clientIdOfCustomValidator,false), it will disable the client validator and also remove the validation message from UI if it displayed.

Here is the javascript function that’s will be called when user select/deselect “Other Contact info” checkbox.
function checkboxChecked(chkLockDelimiter1) {

            var obj_updated_txtDelimiterText = document.getElementById("<%=updated_cvDelimiter.ClientID%>");

            if (chkLockDelimiter1.checked)
                ValidatorEnable(obj_updated_txtDelimiterText, true);
            else
                ValidatorEnable(obj_updated_txtDelimiterText, false);
        }
Here is the demo page link to see in live action.

Leave a comment if you have any question.

Posted in Microsoft Technology Tagged with: , ,
0 comments on “Howto: Fire an ASP.NET Validator from JavaScript
1 Pings/Trackbacks for "Howto: Fire an ASP.NET Validator from JavaScript"
  1. Howto: Fire an ASP.NET Validator from JavaScript | Broken Code…

    […]… Pass own argument as a custom attribute to a specified control from code behind · Howto: Fire an ASP.NET Validator from JavaScript · ASP.NET Custom Validator Validation not firing · Can’t drag and drop Song/Video file in iTunes…

Ads