Following this tutorial , in which the same form is shown, but divided into three steps:
How do I get those required
from the first step to respect each other?
That they are not clickable siguiente
(to go to the 2nd step), but rather validate that they are empty.
Following a comment on the same post, it is recommended to make the following script:
jQuery(".next").click(function() {
current_step = jQuery(this).parent();
var inputs = current_step.find("input")
var countFails = 0;
/**aqui busca si hay informacion el validationMesagge, el cual solo esta lleno cuando el campo esta mal diligenciado*/
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].validationMessage != "Esperalee") {
countFails++;
}
}
/*solo vamos a pasar al siguiente cuando ningun input tenga error*/
if (countFails == 0) {
next_step = jQuery(this).parent().next();
next_step.show();
current_step.hide();
setProgressBar(++current);
}
});
But it still ignores the require and only validates at the end when I submit.
I share the executable snippet below, thank you for your attention.
jQuery(".next").click(function() {
current_step = jQuery(this).parent();
var inputs = current_step.find("input")
var countFails = 0;
/**aqui busca si hay informacion el validationMesagge, el cual solo esta lleno cuando el campo esta mal diligenciado*/
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].validationMessage != "Esperalee") {
countFails++;
}
}
/*solo vamos a pasar al siguiente cuando ningun input tenga error*/
if (countFails == 0) {
next_step = jQuery(this).parent().next();
next_step.show();
current_step.hide();
setProgressBar(++current);
}
});
/* Script para mostrar u ocultar segun el paso a paso */
$(document).ready(function() {
var current = 1,
current_step, next_step, steps;
steps = $("fieldset").length;
$(".next").click(function() {
current_step = $(this).parent();
next_step = $(this).parent().next();
next_step.show();
current_step.hide();
setProgressBar(++current);
});
$(".previous").click(function() {
current_step = $(this).parent();
next_step = $(this).parent().prev();
next_step.show();
current_step.hide();
setProgressBar(--current);
});
setProgressBar(current);
// Change progress bar action
function setProgressBar(curStep) {
var percent = parseFloat(100 / steps) * curStep;
percent = percent.toFixed();
$(".progress-bar")
.css("width", percent + "%")
.html(percent + "%");
}
});
#regiration_form fieldset:not(:first-of-type) {
display: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="container">
<h1>Registro de usuarios paso a paso</h1>
<div class="progress">
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<form id="regiration_form" action="action.php" method="post">
<fieldset>
<h2>Paso 1: Crear su cuenta</h2>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="data[email]" placeholder="Email" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" required>
</div>
<input type="button" name="data[password]" class="next btn btn-info" value="Siguiente" />
</fieldset>
<fieldset>
<h2> Paso 2: Agregar detalles personales</h2>
<div class="form-group">
<label for="fName">Nombres</label>
<input type="text" class="form-control" name="data[fName]" id="fName" placeholder="Nombres" required>
</div>
<div class="form-group">
<label for="lName">Apellidos</label>
<input type="text" class="form-control" name="data[lName]" id="lName" placeholder="Apellidos" required>
</div>
<input type="button" name="previous" class="previous btn btn-default" value="Previo" />
<input type="button" name="next" class="next btn btn-info" value="Siguiente" />
</fieldset>
<fieldset>
<h2>Paso 3: Información de contacto</h2>
<div class="form-group">
<label for="mob">Numero Celular</label>
<input type="text" class="form-control" id="mob" name="data[mob]" placeholder="Numero Celular" required>
</div>
<div class="form-group">
<label for="address">Direccion</label>
<textarea class="form-control" name="data[address]" placeholder="Direccion" required></textarea>
</div>
<input type="button" name="previous" class="previous btn btn-default" value="Previo" />
<input type="submit" name="submit" class="submit btn btn-success" value="Enviar" id="submit_data" />
</fieldset>
</form>
</div>
One way you can accomplish what you're trying to do is by using the ValidityState interface to determine if the type element
input
contains the required data.For example, to determine if an element with a constraint
required
complies with it, you can check the status of the elementvalueMissing
's objectvalidity
:which will return
true
if the element contains any value, andfalse
otherwise.If we want to know if the element fulfills all the restrictions, we will use the property
valid
:An example of using these properties in your code would be to apply it to the button's event handler
next
, and switch to the next view only if theinput
current view's type elements are valid:This would be a way to prevent the next view from being displayed if the current one is invalid. It remains to decide if we show the right messages.
You also need to verify the last view, since you don't have the button
next
but you have the buttonsubmit
and apparently you don't perform the proper verification on it.reportValidity()
For this you could use the element methodform
, adding an event handler to the buttonsubmit
:The complete code would look like this:
I hope this helps you solve the problem.