Fre*_*ddy 6 javascript jquery jquery-validate
我有一个多形式,分为三个fieldsets。我试图实现的工作流程是:
fieldset..next按钮。validate().next单击按钮时发生,如果无效(即未填写必填字段),我想向这些inputs添加一个错误类,以便我可以设置它们的样式(添加红色边框)。fieldset.目前我validate()的出现有点小故障。例如在我下面的演示中,执行以下步骤:
address也是必填字段(如 JS 中所定义),但没有显示错误?单击按钮时,我希望显示错误。
现在,假设填写了名字和地址(该字段集中的两个必填字段),下次单击时,由于这些字段有效,我想在下一个字段集中设置动画,但submitHandler不起作用?不确定为什么?
演示:
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");
});
});
});Run Code Online (Sandbox Code Playgroud)
.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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
为此,您必须创建 3 个单独的表单。每个表单都有自己的验证。在一张表单上设置所有 3 个验证将始终导致无效,因为验证处于活动状态并且所需字段未填写。
<div class="form" id="rsvp-form">
<form id="rsvpForm1" action="" method="post">
<!-- fieldset 1 -->
<fieldset id="field1">
<input type="text" name="fname" placeholder="First name*" />
<textarea name="address" placeholder="Address*"></textarea>
<button>Next</button>
</fieldset>
<!--/ fieldset 1 -->
</form>
<form id="rsvpForm2" action="" method="post">
fieldset 2
<!-- fieldset 2 -->
<fieldset id="fieldset2">
<input type="tel" name="phone" placeholder="Phone*" required />
<button>Next</button>
</fieldset>
</form>
<!--/ fieldset 2 -->
<form id="rsvpForm3" action="" method="post">
fieldset 3
<!-- fieldset 3 -->
<fieldset id="fieldset3">
<textarea name="other" placeholder="Enter your note here ..." required></textarea>
<button>Next</button>
</fieldset>
<!--/ fieldset 3 -->
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
其次,删除输入类型按钮并放置一个按钮。最后,将 id 添加到所有 3 个表单中,并使用它来验证提交时的表单。另外,我以对象的形式添加了验证和消息,因为这种方式更具可读性。
jQuery(function($) {
var current_fs, next_fs, previous_fs;
var left, opacity, scale;
var animating;
/*First Form*/
$("#rsvpForm1").validate({
rules: {
fname: {
required: true,
},
address: {
required: true,
// minlength: 5
}
},
messages: {
fname: {
required: "Please enter your firstname",
},
address:{
required: "Please enter your address",
}
},
// if validation is correct, animate in next fieldset
submitHandler: function(form) {
formSubmit(form,'#fieldset1','#fieldset2');
}
});
/*----------*/
/*Second Form*/
$("#rsvpForm2").validate({
rules: {
phone: {
required: true,
minlength: 5,
maxlength: 11
}
},
messages: {
phone:{
required: "Please enter a valid phone number",
matches: "Invalid value",
minlength: "Min length is exceeded",
maxlength: "Max length is exceeded",
}
},
// if validation is correct, animate in next fieldset
submitHandler: function(form) {
formSubmit(form,'#fieldset2','#fieldset3');
}
});
/*-----------*/
/*Third Form*/
$("#rsvpForm3").validate({
rules: {
phone: {
required: true,
matches: "^(\\d|\\s)+$",
minlength: 11,
maxlength: 11
}
},
messages: {
other:{
required: "Please enter a message"
},
},
// if validation is correct, animate in next fieldset
submitHandler: function(form) {
formSubmit(form);
}
});
/*----------*/
function formSubmit(form, current, next){
if (animating) return false;
animating = true;
current_fs = $(current);
next_fs = $(next);
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'
});
}
});
Run Code Online (Sandbox Code Playgroud)
工作示例: https: //jsfiddle.net/mrAhmedkhan/pjgz7cwq/