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 7895055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:32:58+00:00 2026-06-03T07:32:58+00:00

A very strange problem, I have a text box in aspnet web form. I

  • 0

A very strange problem, I have a text box in aspnet web form. I use jquery to validate the textbox. If it doesn’t pass the validation then an red warning displays by the text box.
So far so good if I click anywhere in the page and suppose I intended to type an invalid text for test.

My code:(UPDATED)

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=TextUserName.ClientID%>").blur(function () {
            ValidateUserNameAfterBlur();
        });
    });

    function ValidateUserName() {
        $.ajax({ type: "POST",
            url: "../UserNameWebService.asmx/ValidateUserName",
            data: "{'strUsername': '" + $("#<%=TextUserName.ClientID%>").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (data) {
                return data;
            }
        });
    }

    function ValidateUserNameBeforeSubmitting() {
        isValid = ValidateUserName();
        return isValid;
    }

    function ValidateUserNameAfterBlur() {
        isValid = ValidateUserName();
        $('#TextUserNameError').toggle(!isValid);
    }



</script>

Some other markup code:

 <td class="style4">
            <asp:TextBox ID="TextUserName" runat="server"></asp:TextBox>
        </td>
        <td>
            <span id="TextUserNameError" style="display:none; color:Red; font-weight:bold;">This is not a valid username</span>
        </td>

However there is an asp.net button on the web page. After I clicked the button, the error did show up, but it then disappeared quickly.

The button’s code:

 <p>
    <asp:Button ID="btnSave" runat="server" Text="SaveChanges" OnClick="btnSave_Click"
        CssClass="saveButton" ValidationGroup="answer" OnClientClick="return ValidateUserNameBeforeSubmitting();" />
</p>

I believe that it is caused by postback but no clue.
Appreciate help from experts.

ADDED Image
debug

And
data.d

  • 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-03T07:32:59+00:00Added an answer on June 3, 2026 at 7:32 am

    I believe you are correct, this is happening because when you click the button (I am assuming it is a submit button?) the page performs a postback. A simple way to prevent the page from posting back when javascript validation fails is to have the javascript function return ‘false’. This will prevent the page from performing the postback.

    That said, I am not sure why you are hooking up your user name validation to document.click. It seems like your page will perform the validation each time the user clicks on the page, regardless of whether the value has changed.

    The accepted method of performing client-side validation of user-entered text is when the control in question loses focus. In JQuery, this would be blur(). This way your validation is run only when the user has finished changing the value in the control.

    This would be your button code (note the OnClientClick):

    <asp:Button ID="btnSubmit" runat="server" OnClientClick="return ValidateUserNameBeforeSubmitting();" />
    

    Your javascript should look like this:

        $(document).ready(function () {
            $("#<%=TextUserName.ClientID%>").blur(function () {
                ValidateUserNameAfterBlur();
            });
        });
    
        function ValidateUserName() {
            $.ajax({ type: "POST",
                url: "../UserNameWebService.asmx/ValidateUserName",
                data: "{'strUsername': '" + $("#<%=TextUserName.ClientID%>").val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function (data) {
                    return data;
                }
            });
        }
    
        function ValidateUserNameBeforeSubmitting() {
            isValid = ValidateUserName();
            return isValid;
        }
    
        function ValidateUserNameAfterBlur() {
            isValid = ValidateUserName();
            //DISPLAY ERROR MESSAGE
        }
    

    [UPDATED]

    WORKING EXAMPLE – A value of “123” will return true for validation. Everything else will return false.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.Microsoft.com/ajax/jQuery/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(document).ready(function () {
            $("#TextUserName").blur(function () {
                ValidateUserNameAfterBlur();
            });
        });
    
        function ValidateUserName() {
            return $("#TextUserName").val() == "123";
    
    //      $.ajax({ type: "POST",
    //          url: "../UserNameWebService.asmx/ValidateUserName",
    //          data: "{'strUsername': '" + $("#<%=TextUserName.ClientID%>").val() + "'}",
    //          contentType: "application/json; charset=utf-8",
    //          dataType: "json",
    //          async: false,
    //          success: function (data) {
    //              return data;
    //          }
    //      });
        }
    
        function ValidateUserNameBeforeSubmitting() {
            isValid = ValidateUserName();
            $('#TextUserNameError').toggle(!isValid);
             return isValid;
        }
    
        function ValidateUserNameAfterBlur() {
            isValid = ValidateUserName();
            $('#TextUserNameError').toggle(!isValid);
        }
    
    
    
    </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextUserName" runat="server" />
            <div ID="TextUserNameError" style="display: none;" >Ooops!</div>
            <br />
            <asp:Button UseSubmitBehavior=true ID="btnSubmit" runat="server" OnClientClick="return ValidateUserNameBeforeSubmitting();" />
        </div>
        </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the memcache extension for python, and I have a very strange problem.
I have a very strange problem. Recently I've created an upload page using jquery
I have a very strange problem. Under some elusive circumstances I fail to apply
I have a very strange problem, when I try to var_dump (or print_r )
I have a very strange problem with the Delphi 2006 IDE. If the IDE
We have very strange problem, one of our applications is continually querying server by
I have a very strange problem with using geometry shaders. I made a very
I have a very strange problem in which identical pieces of Javascript are behaving
Im having a very strange problem, i have a complicated view that returns incorrect
I've been having a very strange problem. I have a test class that subclasses

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.