Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3316324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:27:22+00:00 2026-05-17T22:27:22+00:00

I’m trying to implement a .NET Custom Validator that uses $.ajax to query a

  • 0

I’m trying to implement a .NET Custom Validator that uses $.ajax to query a WebMethod on the same page and return a boolean value to indicate whether the result is true or false.

The WebMethod I’m using is really simple

[WebMethod()]
public static bool IsPromoValid(string code)
{
    string promoCode = "ABCDEFG";
    bool result = code.ToLower() == promoCode.ToLower();
    return result;
}

The CustomValidator looks like this

<asp:CustomValidator ID="cvPromoCode" Display="None" ControlToValidate="txtPromoCode" runat="server" ClientValidationFunction="validatePromo"
    ErrorMessage="The promo code you entered is incorrect" OnServerValidate="ValidatePromoCode" />

And the simple $.ajax() ClientValidation function

function validatePromo(src, args) {
    $.ajax({
        type: "POST",
        url: "Register.aspx/IsPromoValid",
        data: "{'code': '" + args.Value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            args.IsValid = msg.d;
        }
    });
}

The problem is that the page validates instantly and doesn’t actually wait for the ajax call to finish. If there are any other errors on the page, it shows the Validation Summary with them, but never shows the error message from the Custom Validator.

I can see the AJAX call being made in Firebug, and it returs the right response (in this case true or false)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:27:23+00:00Added an answer on May 17, 2026 at 10:27 pm

    The easy way is by changing your validate to:

     function validatePromo(src, args) {
        var isValid;
        $.ajax({
            type: "POST",
            url: "Register.aspx/IsPromoValid",
            data: "{'code': '" + args + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (msg) {
                 isValid = msg.d;
            }        
        });
        args.IsValid = isValid;
    }
    

    Take special note of the async:false. The reason your first try didn’t work is that the ajax success callback was not getting called until after the validation scripts had already checked args.IsValid. With async:false, the $.ajax call won’t complete until after the success callback is done.

    The big issue with this is that it now “blocks” whatever js thread is running the validation. In the case of ASP.Net validators, I don’t believe this is an issue but I would test it with a long-running call just to make sure you aren’t screwing up your page for anyone on a slow connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.