I am using the jQuery SmartWizard for one of my projects. I have validations in place via the jQuery Validation plugin which appear to be working. The issue is that I want SmartWizard to stop users from navigating to the next step if validation for the current step failed.
The code below seems to work since I can see the Form on step X is invalid console log entry, but the following return false, which supposedly should stop the user from navigating to the next step, seems to be ignored even though validation clearly failed.
I alternatively tried binding the function to a click event for the sw-btn-next button, which is the button thats clicked to proceed to the next step, but this does not seem to work either.
jQuery 3.2.1 // Bootstrap 3.3.7 // SmartWizard 5.1.1 (latest) // jQuery Validation 1.19.3 (latest)
jQuery Validation
// set validation rules for entire form
$('#addNewUser').validate({
debug: true,
errorElement: 'span',
errorClass: 'form-text form-error text-red',
focusInvalid: false,
rules: {
'first_name': {
required: true,
minlength: 3
},
'last_name': {
required: true,
minlength: 3,
},
'password': {
required: true,
minlength: 5,
maxlength: 25
},
'password_repeat': {
required: true,
minlength: 5,
maxlength: 25,
equalTo: '#password'
}
},
messages: {
'first_name': 'Please enter a valid first name'
}
});
SmartWizard
// ###################################
// # jQuery SmartWizard plugin
// ###################################
$('#smartwizard').smartWizard({
selected: 0, // Initial selected step, 0 = first step
theme: 'default' // theme for the wizard, related css need to include for other than default theme
// when changing steps...
}).on("stepContent", function(e, anchorObject, stepIndex, stepDirection) {
// check if current step is Step 5 and enable submit button
$('#newUserSubmit').prop('disabled', stepIndex !== 4);
console.log('Navigated to stepIndex ' + stepIndex + ' moving in stepDirection ' + stepDirection);
// check form validation
var elmForm = $('#form-step-' + stepIndex);
if (stepDirection == 'forward' && elmForm) {
if ($('#addNewUser').valid()) {
console.log('Form on Step ' + stepIndex + ' is valid');
return true;
} else {
console.log('Form on Step ' + stepIndex + ' is invalid');
return false;
}
}
});
// when clicking NEXT button...
$('.sw-btn-next').click(function() {
console.log('Next button clicked.')
});
After lots of trial and error, I finally fixed the issue by moving the validation check from SmartWizard's
stepContentevent to theleaveStepevent. Please note that the often referenced$('.sw-btn-next').click(function()does not work either, it has to beleaveStep.Above code works provides a working SmartWizard step-by-step wizard with jQuery Validation for each step by calling validation for the entire
formwhen the user navigates away from the current step. Additionally, the script places an extraSubmitbutton to the wizard which is automatically disable unless the user has reached the final step of the wizard.There is surprisingly little documentation for this particular use-case, I hope this helps anyone else who experiences issues with SmartWizard & jQuery Validation.