1

I have a registration form with several validator controls. I am checingk the username availability via AJAX (using jQuery, NOT an UpdatePanel). If the username is taken, I would like to make the page invalid like the other ASP.NET validators do from my JavaScript function. Is this possible? Examples?

Here is my current functionality:

// In head...
<script type="text/javascript">
$(document).ready(function () {
    $('#<%= username.ClientID %>').blur(function () {

        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: 'MyService.asmx/CheckAvailability',
            dataType: 'json',
            data: '{ "username": "' + username + '" }',
            success: function (response) {

                // stuff

                // If username is taken, invalidate

            }, error: function () { /* stuff */ }
        });
    });
});
</script>

// The markup...
<dl><dt>Username</dt>
<dd>
    <asp:textbox id="username" runat="server" />
    <asp:requiredfieldvalidator id="usernameRequiredValidator" runat="server"
        controltovalidate="username"
        errormessage="Required"
        display="Dynamic" />
    <span id="avail_response"></span>

<!-- More stuff... -->
3
  • 3
    Could you please explain what you mean with 'make the page invalid' ? Or perhaps give an example . . Commented Feb 14, 2011 at 15:16
  • When one of your form elements is "invalid", the user can not submit the form. This is automatically handled w/ASP.NET validation controls. I would like to hook this functionality from my AJAX function. Commented Feb 14, 2011 at 15:18
  • Instead of invalidating the page, is it enough to invalidate the submit button in the page? Commented Feb 15, 2011 at 6:32

1 Answer 1

2

This link provides an in-depth explanation of ASP.Net validation. Scroll down to the paragraph 'The Client-Side API' where it explains that you can set the Page_IsValid variable. Be warned however, there is no guarantee that this API will stay the same in future versions of .NET so check for the existence of Page_IsValid.

EDIT :

Sorry, in my previous comment I was in a bit off a hurry and I just answered your question without looking at the bigger picture. I just reread your question and indeed, it will do nothing because when you the user clicks the submit button, it triggers a revalidation of all validators. Because the textbox username has a value, your requiredvalidator is valid and the form submits.

I think that a better solution in the long run (and one that doesn't directly call javascript functions of .NET) is the following :

  1. Add a hidden field
  2. Create a requiredfieldvalidator for that hidden field
  3. Place the requiredfieldvalidator behind the first requiredvalidator and give it the same errormessage as that requiredvalidator
  4. Place some value in the hidden field in your success function if the username is available, in all other cases, make the hidden field empty.

It's a bit of a detour but it's future proof & easier than figuring out how to do it with the javascript functions of the .NET validator framework.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. But how can I use the Page_IsValid object in my javascript function above?
In your success callback function, use 'if (typeof Page_IsValid != 'undefined') { Page_IsValid = false; }' I think it should work but I'm not certain (I cannot check at the moment).
Yea, I tried setting Page_IsValid manually, but it does nothing
link in Post isn't valid anymore. But the gist is here. :)
@winner_joiner I fixed the link (& 2 spelling errors). Thanks for informing me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.