Problem with jquery to prevent the page of a login from being reloaded?

1

I have the following jquery to validate the input data of a login, but I show the msj and the page is reloaded and the false return and e.preventDefault () does not do its function.

      $('#btnlog').closest('form').on('submit', function (e) {
            e.preventDefault();
            $('#btnlog').attr('disabled', true);
            this.submit(); // ahora hace el submit de tu formulario.
            var email = $('#email').val();
            var contraseña = $('#contraseña').val();

            if (email == '' || contraseña == '') {
                e.preventDefault();
                $('#msjviewbag').empty();
                $('.espacio').hide();
                $('#btnlog').attr('disabled', false);
                $('#errorlog').html('Los Campos No Deben Estar Vacios');
                return false;
            }

            if ($('#email').val() != '') {
                if ($('#email').val().indexOf('@@', 0) == -1 || $('#email').val().indexOf('.', 0) == -1) {
                    e.preventDefault();
                    $('#msjviewbag').empty();
                    $('.espacio').hide();
                    $('#btnlog').attr('disabled', false);
                    $('#errorlog').html('El Formato de Email No es Correcto');
                    return false;
                }
            }
        });
    
asked by Alcides Salazar 10.12.2016 в 19:17
source

1 answer

2
  

The error is that you are forcing the sending of the form when doing this.submit();

One solution could be the following:

  $('#btnlog').closest('form').on('submit', function (e) {
        $('#btnlog').attr('disabled', true);
        var email = $('#email').val();
        var contraseña = $('#contraseña').val();
        var error = '';

        if (email == '' || contraseña == '') {
            error = 'Los Campos No Deben Estar Vacios';
        }

        if ($('#email').val().indexOf('@', 0) == -1 || $('#email').val().indexOf('.', 0) == -1) {
            error = 'El Formato de Email No es Correcto';
        }

        if (error) {
            e.preventDefault();
            $('#msjviewbag').empty();
            $('.espacio').hide();
            $('#btnlog').attr('disabled', false);
            $('#errorlog').html(error);
            return false;
        }
    });
    
answered by 10.12.2016 / 19:58
source