I have a multi-form which is divided into three fieldsets
. The workflow I'm trying to achieve is:
- User is presented form, which has fields from the first
fieldset
. - User completes what they can and hits the
.next
button. validate()
occurs on.next
button click, if invalid (i.e. required fields are not filled in), I want to add an error class to thoseinput
s so I can style them (add red borders).- If they are correct (all fields are filled in that are required), then continue on animating in the next
fieldset
.
At the moment my validate()
is showing up a bit glitched. For example in my demo below, perform the following steps:
- Click the next button (check the console to see the message to confirm the button has been clicked).
- Notice how no error messages have come up?
- Now click on the first name field. Notice how the error message now shows up?
- This is also weird as
address
is also a required field (as defined in the JS) but doesn't show up with errors?
On the button click, I want the errors to show up.
Now, let's say first name and address (two required fields in that fieldset) are filled out, on next click, since those fields are valid, I want to animate in the next fieldset, but the submitHandler
isn't working? Unsure why?
Demo:
jQuery(function($) {
var current_fs, next_fs, previous_fs;
var left, opacity, scale;
var animating;
$(".next").click(function() {
console.log('next is clicked');
$("form").validate({
rules: {
// name : param
fname: "required",
address: "required",
phone: {
required: true,
matches: "^(\\d|\\s)+$",
minlength: 11,
maxlength: 11
}
},
messages: {
fname: "Please enter your firstname",
address: "Please enter your address",
phone: "Please enter a valid phone number"
},
// if validation is correct, animate in next fieldset
submitHandler: function(form) {
if (animating) return false;
animating = true;
current_fs = $(this).parent();
next_fs = $(this).parent().next();
$("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
next_fs.show();
current_fs.animate({
opacity: 0
}, {
step: function(now, mx) {
scale = 1 - (1 - now) * 0.2;
left = (now * 50) + "%";
opacity = 1 - now;
current_fs.css({
'transform': 'scale(' + scale + ')',
'position': 'absolute'
});
next_fs.css({
'left': left,
'opacity': opacity,
'height': 'auto',
'padding': '60px 50px'
});
},
duration: 800,
complete: function() {
current_fs.hide();
animating = false;
},
easing: 'easeInOutBack'
});
}
});
$('input').blur(function() {
$("form").validate().element("input");
});
});
});
.form {
min-height: 800px;
user-select: none;
overflow: hidden;
}
.form form#rsvpForm {
width: 600px;
margin: 50px auto;
text-align: center;
position: relative;
}
.form form#rsvpForm fieldset {
background: white;
border: 0 none;
border-radius: 3px;
box-shadow: 0 0 15px 1px rgba(0, 0, 0, 0.4);
padding: 60px 50px;
box-sizing: border-box;
position: relative;
width: 100%;
display: block !important;
}
.form form#rsvpForm fieldset:not(:first-of-type) {
opacity: 0;
}
.form form#rsvpForm input,
.form form#rsvpForm textarea {
padding: 15px;
border: 1px solid #ccc;
border-radius: 3px;
margin-bottom: 10px;
width: 100%;
box-sizing: border-box;
outline: none;
}
.form form#rsvpForm input.error,
.form form#rsvpForm textarea.error {
border: 1px solid red;
}
.form form fieldset .error__message{
display: none;
}
.form form fieldset.has-error .error__message{
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js?ver=5.3.2'></script>
<script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js'></script>
<div class="form" id="rsvp-form">
<form id="rsvpForm" action="" method="post">
<!-- fieldset 1 -->
<fieldset>
<input type="text" name="fname" placeholder="First name*" />
<textarea name="address" placeholder="Address*"></textarea>
<input type="button" id="confirm" name="next" class="next" value="Next" />
</fieldset>
<!-- fieldset 2 -->
<fieldset>
<input type="tel" name="phone" placeholder="Phone*" required />
<input type="button" id="confirm" name="next" class="next" value="Next" />
</fieldset>
<!-- fieldset 3 -->
<fieldset>
<textarea name="other" placeholder="Enter your note here ..." required></textarea>
<input type="submit" name="submit" class="submit" value="Submit" />
</fieldset>
</form>
</div>