Disable Asp.net form validators with Javascript API

by pencilvac 2/17/2012 12:00:00 PM

You may come across the necessity in a project to disable or enable server side asp.net form validators because of some form of client side action with javascript. In my scenario I had to either display or hide a state dropdownlist ( a telerik control radcombobox) and show a region/province text box in a customer input form depending on whether the user was from the USA or another country. When the user was outside of the U.S. we obviously need to remove the state recquired field validator.

In order to do so I took advantage of the very robust Telerik radcombobox javascript api which included the onClientLoad and OnClientSelectedIndexChanged Client side functions which I attached to my countries radcombobox.

<telerik:RadComboBox  Width="142" ID="ddlCountries" OnClientLoad="checkCountry" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged"  runat="server" MarkFirstMatch="True" />

onClientLoad runs any javascript you put inside of it immediately after the drop down is done loading. OnClientSelectedIndexChanged runs, well, right after the user picks a new value in the drop down.

OnClientLoad function :

    function checkCountry(sender) {

        var value = document.getElementById('<%=ddlCountries.ClientID  %>').value;

        if (value !== "United States") {

            var stateValidator = document.getElementById('<%= valState.ClientID  %>');
            ValidatorEnable(stateValidator, false);

        }
        else {

            var stateValidator = document.getElementById('<%= vaState.ClientID  %>');
            ValidatorEnable(stateValidator, true);

        }
    }

Basically the function onload checks to see if the value of the drop down selected is the United States or not. If it is the U.S. then the 

ValidatorEnable function take the parameter of the server ID of the validator you either want to enable or disable as well as whether you want

to enable or disable it. In order to retrieve the Client side ID of the validator I threw the javascript functions within a Telerik Code block.

 

Like magic you can disable and/or enable your asp.net server side validators from client side javascript using the Telerik javascript API.

Tags:

ASP.NET AJAX | JavaScript | Telerik

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  October 2018  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2018

Sign in