1
I am trying to create a simple validation of two fields, where I only check whether there is a value or not, but the second if only runs after the first one is checked.
I wanted the two of them to be shot at the same time, which would be the most practical way to do it?
Follow the example of my current validation:
$(document).ready(function(){
$('#formLogin').on('submit', function(e){
if($('#txtLogin').val() == ''){
e.preventDefault();
if (!$('.err-text').length) {
$('#txtLogin').closest('.login__column').append(`<span class="err-text">*E-mail incorrecto.
<br>Ingresa un E-mail válido para participar.</span>`);
}
} else {
$('#txtLogin').next('.err-text').remove();
}
if($('#txtSenha').val() == ''){
e.preventDefault();
if (!$('.err-text').length) {
$('#txtSenha').closest('.login__column').append(`<span class="err-text">*Contraseña incorrecta.
<br>Ingresa una contraseña válida para participar.</span>`);
}
} else {
$('#txtSenha').next('.err-text').remove();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formLogin">
<div class="login__column">
<input type="text" id="txtLogin">
</div>
<div class="login__column">
<input type="text" id="txtSenha">
</div>
<button type="submit">Enviar</button>
</form>
Legal your solution
– gabrielfalieri