Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Ajax CTK w/jQuery - Req Field Val shows valid when not valid clientside

$
0
0

Hi Everyone,

I current have a page with the ajax control toolkit setup & jquery on the page. As such, my jquery ready is firing twice. I have custom javascript bound to the button click which checks a checkbox on the form and then validates a specified set of groups.

Keep in mind the javascript on the page is getting called twice. In debugging my javascript, on the first execution of the javascript the validator correctly identifies the field as invalid but the second time around incorrectly says it's valid and lets the request go through to the server (which kicks back the page as not valid).

This is occuring for more than one validator and in more than one validation group butonly the second time the code is executed.

As far as I can tell, the double execution is occuring because of the toolkit as when I remove the toolkit - it no longer fires twice. I only have 1 reference to jquery placed within my toolkit script manager on my sitemap but I do pull in other javascript files on specific pages without using the script manager and this issue occurs with each button click when invalid fields are present.

If I removed the jquery reference from the script manager and include it through normal scripts tags - the issue still occurs but not on every click.

Javascript: 

$(document).ready(function ()
{
        $('#btnRegister').click(function ()
        {
            var bIsValid = true;
            var bVal = true;
            // Validate Core Items
            bVal = CheckValidationGroup("CoreValGroup");
            // If a negative result is returned, preserve it.
            if (bVal == false)
            {
                bIsValid = false;
            }

            // Validate Address Form
            // Core components
            bVal = CheckValidationGroup("AddrValGroup");
            // If a negative result is returned, preserve it.
            if (bVal == false)
            {
                bIsValid = false;
            }
            if ($("#chkBox").is(':checked'))
            {
                bVal = CheckValidationGroup("OtherValGroup");
                // If a negative result is returned, preserve it.
                if (bVal == false)
                {
                    bIsValid = false;
                }
            }
            else
            {
                // Now Validate
                bVal = CheckValidationGroup("LocalValGroup");
                // If a negative result is returned, preserve it.
                if (bVal == false)
                {
                    bIsValid = false;
                }
            }

            return bIsValid;
        });
});

function CheckValidationGroup(valGroup)
{
    var bIsValid = true;
    for (i = 0; i < Page_Validators.length; i++)
    {
        var val = Page_Validators[i];
        if (val.validationGroup == valGroup)
        {
            ValidatorValidate(Page_Validators[i], valGroup, null);
            if (Page_Validators[i].isvalid == false)
            {
                bIsValid = false;
            }
        }

    }
    return bIsValid;
}

HTML Snippet: (not full page, but one validator that is returning IsValid = true when it's not valid.)

<span class="spnReqAsterisk">*</span><span class="spnRequired">Security Word #5</span><span><asp:RequiredFieldValidator ID="valSecAnswer5" runat="server" Display="Dynamic" ValidationGroup="CoreValGroup" ControlToValidate="txtSecAnswer5"
Text="REQUIRED" ErrorMessage="Security Answer #5 is required!" CssClass="validator" /></span><asp:TextBox ID="txtSecAnswer5" runat="server" MaxLength="50"></asp:TextBox><asp:Button runat="server" Text="Register" ID="btnRegister" ClientIDMode="Static" CausesValidation="true" class="btnRegister" OnClick="btnRegister_Click" />

I hope I've provided enough material as I've been searching for a few days now and haven't found a correct solution - either stop jquery from firing twice or identify why the second firing returns a valid result.

Thank you,

Chad


Viewing all articles
Browse latest Browse all 5678

Trending Articles