我到处搜索,但似乎找不到解决方案。我正在尝试创建一个表单,其中它有 3 个字段集,一次只有一个字段集可见。我有 onkeyup 验证工作,如下所示:
$("#contactInfoForm").validate({
debug:true,
onkeyup: false,
rules: {
firstName: {
required: true,
loginRegex: true,
minlength: 2
},
lastName: {
required: true,
minlength: 2
},
Email:{
required: true,
email: true
},
Email_confirm:{
required: true,
email: true,
equalTo: "#Email"
},
streetAddressBilling:{
required: true,
minlength: 3
},
billingCity:{
required: true,
digits: true,
minlength: 3
},
state:{
required: true
},
zip:{
required: true,
digits: true,
minlength: 3
},
phone:{
required: true,
digits: true,
minlength: 3,
phoneUS: true,
},
prefix:{
required: true,
digits: true,
minlength: 3
},
landline:{
required: true,
digits: true,
minlength: 4
}
},//end of rules
groups: {
DateofBirth: "phone prefix landline"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "phone" || element.attr("name") == "prefix" || element.attr("name") == "landline")
error.insertAfter("#landline");
else
error.insertAfter(element);
},
messages:{
firstName: {
required: "Please enter first name",
lettersonly: "No numbers please",
loginRegex: "Login format not valid",
minlength: "Use at least 2 letters"
},
lastName: {
required: "Please enter last name",
lettersonly: "No numbers please",
minlength: "Use at least 2 letters"
},
Email: {
required: "Please enter your email",
email: "Please enter a valid email address"
},
Email_confirm: {
required: "Please enter your email",
email: "Please enter a valid email address",
equalTo: "Email addresses do not match"
},
streetAddressBilling:{
required: "Please enter your street address",
minlength: "Use at least 3 letters"
},
billingCity:{
required: "Please enter your city",
minlength: "Use at least 3 letters"
},
state:{
required: "Please select your state."
},
zip:{
required: "Please enter your zip code",
digits: "Please only use numbers",
minlength: "Use at least 5 letters"
},
phone:{
required: "Please enter your phone number",
digits: "Please only use numbers",
minlength: "Use 3 numbers"
},
prefix:{
required: "Please enter your phone number",
digits: "Please only use numbers",
minlength: "Use 3 numbers"
},
landline:{
required: "Please enter your phone number",
digits: "Please only use numbers",
minlength: "Use 4 numbers"
}
}//end of messages
});//end of contact info form validation rules
我要做的下一件事是在每个字段集的末尾有一个继续按钮。该按钮应该检查该字段集中的表单,如果它们被验证,则打开下一个字段集。以下是字段集的 HTML:
<form onsubmit="return submitForm(this)" method="post" id="contactInfoForm" name="contactInfoForm" action="#">
<fieldset>
<legend class="accessible-text">Address</legend>
<ol>
<li class="fullInput">
<label id="caption_address1" for="address1">
Address
</label>
<input type="text" autocomplete="off" aria-required="true" placeholder="Street Address" class="fullInput" maxlength="30" id="address1" name="address1">
</li>
<li class="no-label fullInput">
<label class="accessible-text" id="caption_address2" for="address2">
Billing Address Line 2
</label>
<input type="text" maxlength="40" autocomplete="off" aria-required="false" placeholder="Street Address Line 2(Optional)" class="fullInput" id="address2" name="address2">
</li>
<li class="no-label">
<label class="accessible-text" id="caption_city" for="city">
City
</label>
<input type="text" aria-required="true" placeholder="City" maxlength="30" id="city" name="city">
</li>
<li class="no-label">
<label class="accessible-text" id="caption_state" for="state">
State
</label>
<select aria-required="true" class="required" name="state" id="state">
<option value="">State</option>
</select>
</li>
<li class="no-label">
<label class="accessible-text" id="caption_zipCode" for="zipCode">
ZIP
</label>
<input type="text" autocomplete="off" aria-required="true" placeholder="20151" name="zipCode" id="zipCode" maxlength="5">
</li>
<li>
<label class="fullInput" id="caption_phone" for="phone">
Main Contact Number <a href="">Why we need this</a><br>
<span class="accessible-text">Please enter your area code</span>
</label>
<input type="text" autocomplete="off" aria-required="true" placeholder="703" name="phone" id="phone" maxlength="3">
<label class="accessible-text" id="caption_prefix" for="prefix">
Enter the next 3 numbers
</label>
<input type="text" autocomplete="off" aria-required="true" placeholder="555" name="prefix" id="prefix" maxlength="3">
<label class="accessible-text" id="caption_landline" for="landline">
Enter the last 4 numbers
</label>
<input type="text" autocomplete="off" aria-required="true" placeholder="5555" name="landline" id="landline" maxlength="4">
</li>
</ol>
</fieldset>
</form>
这是按钮的 JS,我使用 jquery ui 进行褪色:
$("#button").on("click", function(event){
if($('#contactInfoForm').validate({
rules: {
firstName: {
required: true,
loginRegex: true,
minlength: 2
},
lastName: {
required: true,
minlength: 2
},
Email:{
required: true,
email: true
},
Email_confirm:{
required: true,
email: true,
equalTo: "#Email"
},
streetAddressBilling:{
required: true,
minlength: 3
},
billingCity:{
required: true,
digits: true,
minlength: 3
},
state:{
required: true
},
zip:{
required: true,
digits: true,
minlength: 3
},
phone:{
required: true,
digits: true,
minlength: 3,
phoneUS: true,
},
prefix:{
required: true,
digits: true,
minlength: 3
},
landline:{
required: true,
digits: true,
minlength: 4
}
},//end of rules
}))
{ //checks if it's valid
if($(this).valid()) {
$('#about_you_ver2').toggleClass("mod");
$('#nameFieldsetEdit').toggleClass("hide");
$('#name').fadeOut('xslow', function(){
//$(this).effect("highlight", {}, 2000);
}),
$('#billing_address').fadeIn('xslow', function(){
//$(this).effect("highlight", {}, 2000);
}),
$('#billing_container p').fadeIn('xslow', function(){
//$(this).effect("highlight", {}, 2000);
})
}//end of valid direction
}
else{
alert("I'm not valid");
}
});//end of onclick for button
在单击按钮而不输入任何信息时,我被困在表单通过验证的地方。我怎样才能完成这个问题?