How to stop ajax spinner on ASP.NET MVC register form if client side validation fails

Evonet

I have an MVC5 register page with 2 forms - one to register via Facebook, and one to register via a local account.

My problem is that I've added ajax progress indicators to both forms, but I can't get the spinner to stop spinning on client validation failure.

The following code is my login with Facebook button, and when the user clicks it the button is replaced by a spinning ajax indicator:

    @using (Ajax.BeginForm("ExternalLogin", "Account", null, new AjaxOptions { LoadingElementId = "loader" }, new { id = "fbForm" }))
    {
        @Html.AntiForgeryToken()

        <p class="text-center">
            <button type="submit" id="Facebook" name="provider" value="Facebook" title="Log in using your Facebook account">Sign in with Facebook</button>
            <div id="fbLoader" style="display:none; text-align:center">
                <img src="/images/Ajaxloader.gif" height="31" width="31" />
            </div>
        </p>


    $(document).ready(function () {

        $('#fbForm').submit(function () {
            $('#fbForm button').hide();
            $('#fbForm #fbLoader').show();

            return true;
        });

    });

I've tried to use the exact code for my local login registration, and it works perfectly is server side validation fails (such as when the account name alreay exists), but for client side validation (such as confirm password doesn't match, or password not long enough) the ajax spinner starts, but never disappears.

I've tried adding Microsoft.jQuery.Unobtrusive.Ajax and using OnBegin and OnSuccess to work around this, but then while I got the local login working the way I wanted, the Facebook login broke with the following error:

XMLHttpRequest cannot load https://www.facebook.com/dialog/oauth?response_type=code&client_id=57560734…Igt2j28GIN_o6bk5MTZTtv_u8NxSzz1_NvnohC9-HqpzJMBrhxT737xJmNLCY4q_Gg0UORz5UH. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:44300' is therefore not allowed access. 
Evonet

I fixed this by using the following code for my local form:

        $("#btnSubmit").click(function (evt) {
            var options = {};
            options.beforeSend = function () {
                $('#localForm #btnSubmit').hide();
                $('#localForm #localLoader').show();
                $("#localForm").submit();
            };
            options.complete = function () {
                $('#localForm #btnSubmit').show();
                $('#localForm #localLoader').hide();
            };
            $.ajax(options);
            evt.preventDefault();
        });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.Net Core MVC - Run client side script/function after form submit data success

ASP.Net Core MVC - Client-side validation for custom attribute

Validation Controls for ASP.NET Server side or client side?

ASP.NET MVC ValidationAttribute causes invalid client side validation rules

How to stop Form submit if the validation fails

How to in ASP.NET MVC prevent jquery ajax submit if validation fails

Disable client side form validation in ASP.NET Core

How to use Globalize.js to configure culture for client side validation in ASP.NET Core MVC

Configure culture for client side validation in ASP.NET Core MVC

Client side validation "required" with conditional logic to kendo DropDownList from the ASP.NET MVC UI Api

How to add required field validation on client side with custom message for Radio Button in ASP.Net MVC?

ASP.NET Core Unobtrusive Client Side Validation Form onsubmit fires even on validation failure

How to do client side validation of listbox in asp.net mvc application

How to trigger ASP .NET Core MVC client-side validation without submitting

Conditional client side validation in ASP.NET MVC Core 3.1

How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC?

Client side validation in asp.net mvc3

client side form validation

Client side validation for custom validator asp.net MVC 4

Ajax post a form (validation) in MVC AsP.net?

Asp.net MVC 4 Client Side Validation Not Work

Issue with jQuery datepicker and client side validation for dates in ASP.net MVC 4

How to invoke client side form validation in MVC 5 without a Submit button

ASP validation client side

Submit form via AJAX but keep client side validation

Adding client side validation failures to ASP.NET MVC ModelState

ASP.NET MVC Ajax submit form with AntiforgeryToken and Serialized Form data with Validation

asp mvc core 3 Client side validation for a custom attribute validation

How can I create a form with validation in ASP.NET MVC?