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

  • SEARCH
  • Home
  • 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 7811873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:19:24+00:00 2026-06-02T04:19:24+00:00

I have a jquery ajax call asp.net web service question. I want to validate

  • 0

I have a jquery ajax call asp.net web service question. I want to validate a textbox in asp.net web page.
The validator is:

<asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName"
                                ErrorMessage="Minimum of 6 (six) alphanumeric characters." 
                 ClientValidationFunction="ValidateUserName" Display="Dynamic"
                                ValidateEmptyText="True" ></asp:CustomValidator>

The jquery code is(updated 2nd):

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script src="../jquery-1.7.2.min.js" type="text/javascript"></script>
<script>
$.ajax({
    type: "POST",
    url: "UserNameWebService.asmx/ValidateUserName",
    data: "{'strUsername': " + $("#TextUserName").val() + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});
</script>
<div>
    General user information</div>
<p>
</p>
<table cellpadding="2">

The web service code is:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class UserNameWebService : System.Web.Services.WebService
{

    [WebMethod]
     public bool ValidateUserName(string strUsername)
    {
        string UserNameCreated = strUsername;
        string AD_Server = System.Configuration.ConfigurationManager.AppSettings["AD_Server"];
        DirectoryEntry entry = new DirectoryEntry(AD_Server);
        entry.AuthenticationType = AuthenticationTypes.Secure;

        DirectorySearcher deSearch = new DirectorySearcher(entry);
        deSearch.Filter = "(&(objectClass=user)(samaccountname=" + UserNameCreated + "))";

        SearchResultCollection results = deSearch.FindAll();
        Match match = Regex.Match(UserNameCreated, @"^[a-zA-Z0-9]{6,}$", RegexOptions.IgnoreCase);
        if (results.Count > 0)
            return false;
        else if (match.Success)
            return true;
        else
            return false;
    }    }

But I got an error:

ValidateUserName is undefined.

Please help me to correct the error.

Thank you very much!

  • 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-06-02T04:19:25+00:00Added an answer on June 2, 2026 at 4:19 am

    There are actually several problems with your code.

    1) $("#TextUserName") will not work in this context because asp.net will render the server-side TextBox control with a different ID. You would need to do this instead:

    data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
    

    2) The json in your data attribute is not formatted correctly, you need single quotes ' around the value like this:

                           |                                              | 
                           V                                              V
    data: "{'strUsername': '" + $("#<%=TextUserName.ClientID%>").val() + "'}",
    

    3) You need to put your Jquery ajax call inside a function, which in your case is called ValidateUserName. It takes two parameters source and args. The responsibility of this function is to set the value of args.IsValid to either true or false. So you will need to provide a function to be called when the ajax call succeeds that can perform this logic. Like this:

    function ValidateUserName(source, args) {
        $.ajax({
            type: "POST",
            url: "UserNameWebService.asmx/ValidateUserName",
            data: "{'strUsername': '" + args.Value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (result) {
                args.IsValid = result.d;
            }
        });
    }
    

    4) As you can see in the code above, you don’t actually need to use jquery to get the value from the textbox because you can access it like this args.Value.

    5) You need to add async: false otherwise by the time IsValid is set, the code that sets the visibility of the message will already have been executed, and so nothing will happen.

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

Sidebar

Related Questions

I have a jquery ajax call asp.net web service but it is not working.
I have a web service that returns this string via the jQuery $.ajax() call
I want to call some web-service webmethods on asp.net submit button's click in order
I have an ASP.Net MVC 3 application that uses a jQuery .ajax call to
On my main page I have this jquery code which does an ajax call
I am having issues calling an ASP.NET web service in JQuery. I am basically
Actually I am trying to learn jQuery Ajax calls to asp.Net webservices. I have
I've a problem...I use jQuery ajax to call a web service that returns XML.
I have a ASP.Net web app with jquery implemented on the client side. within
I have an ASP.NET MVC 3 (Razor) Web Application, with a particular page which

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.