Enable the invalid Foundation 6 input via JS/jQuery

Asked

Viewed 56 times

0

I’m developing a logins registration form.

The login must be unique. Every time a login is typed in the field the onblur event makes a connection in ajax where it is checked if in the database there is such login typed.

This form is made in Foundation 6. Follow the form code.

It turns out that when the login in the database is identified, I cannot make the form 'lock' until the user enters a valid login. I did in jQuery activating the error classes of the fields but, even so, the user can proceed with the registration.

Is there any way to activate the Foundation 6 form error via JS/jQuery?

HTML: (Foundation 6)

    <form method="POST" name="contrato" action="teste.php" target="_blank" data-abide novalidate>
          <label id="labelTeste" for="teste">Login do Usuário *
            <input name="teste" pattern="[A-Za-z]{3}[.][A-Za-z]{3}" maxlength="100" id="teste" type="text" placeholder="nome.sobrenome" aria-describedby="helpLoginUsuario" required>
            <span id="spanTeste" class="form-error"> Campo obrigatório</span>
          </label>
          <p class="help-text" id="helpTeste">Login do usuário</p>
    </form>

JS/jQuery/Ajax:

$('#teste').on('blur', function(){
        var url = 'ajax/teste.php';
        var usuario = $('#teste').val();
        $.ajax({
            type : 'post',
            data : {
                "usuario" : usuario
            },
            url : url,
            cache : false,
            dataType : 'html',
            timeout : 120000,
            success : function(data) {
                if(data > 0)
                {
                    $('#labelTeste').addClass('is-invalid-label');
                    $('#teste').attr('data-invalid', '');
                    $('#spanTeste').addClass('is-visible');
                    $('#spanTeste').text('Este usuário já existe, por favor, utilize outro!');
                }   else {
                    $('#labelTeste').removeClass('is-invalid-label');
                    $('#teste').removeAttr('data-invalid');
                    $('#spanTeste').removeClass('is-visible');
                    $('#spanTeste').text('Campo obrigatório');
                    $('#helpTeste').text('Usuário válido!');
                }
            },
        });
    });
  • "It turns out that when the login in the database is identified, I can’t make the form 'lock' until the user enters a valid login" - Can’t do what? What would "lock" the form?

  • Make sure the form doesn’t go to its next step while it’s logged in the same. Even with Foundation error classes active, when user clicks the Submit button the form advances with the error.

  • I think that’s the problem novalidate in the form, because if the login is invalid, you could check the entries and invalidate the form - this way the user will not be able to proceed.

  • If I remove the novalidate, the error messages foudantion gestures are incorrect.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.