我在一个页面上有n个表单。每个人都有两个电子邮件地址字段(普通字段和验证)。 我想要做的是为这两个字段设置一个equalTo规则。我现在正在做的是:
$(".contactform").validate({
onsubmit: true,
onkeydown: false,
onkeyup: false,
onfocusin: false,
onfocusout: false,
onchange: false,
onclick: false,
rules: {
email_veri: {
equalTo: ".email"
}
}
});
但这显然不起作用,因为电子邮件和email_veri的ID在每种形式都不同。我读到可以直接在字段上设置验证规则,但我没有找到方法。我希望有人可以提供帮助。
答案 0 :(得分:2)
这可能很有用,来自http://www.epalla.com/2009/12/jquery-validation-with-multiple-forms/的“多个(大多数)相同形式的jQuery验证”
他们的答案是分别验证每张表格:
$(".question_form").each(function() {
$(this).validate(
rules: {
email_veri: {
equalTo: ".email"
}
);
});
答案 1 :(得分:1)
Hiya为您创建了一个非常简单的演示:http://jsfiddle.net/bY5P6/
如果您要输入不同的电子邮件和电子邮件验证字段,那么您将获得equal to
消息验证。
进一步的文档:http://docs.jquery.com/Plugins/Validation/Methods/equalTo
或多种表格请见:Using JQuery Validate Plugin to validate multiple form fields with identical names
如果您使用多种表单,请确保引用:
验证要求字段名称除了单选按钮外是唯一的 和复选框。
:)!
Jquery代码
//================================= AZIONE FORM
$('#submit').click(function() {
$('#mailer').submit();
return false;
});
//================================= VALIDAZIONE FORM
$('#mailer').validate({
focusInvalid: false,
debug: true,
rules: {
name: {
required: true
},
email_address: {
required: true,
email: true
},
email_address_veri: {
required: true,
email: true,
equalTo: ".email"
},
request: {
required: true
}
},
messages: {
name: {
required: 'name required'
},
email_address: {
required: 'email required',
email: 'email address is not valid'
},
email_address_veri: {
required: 'email required',
email: 'email address verification is not valid',
equalTo: "Please enter the same email as above"
},
request: {
required: 'request required'
}
}
});
<强> HTML 强>
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="#" method="post" id="mailer" autocomplete="off">
<label>
<span>name</span>
<input type="text" name="name" required="required" value="" />
</label><br /><br />
<label>
<span>email</span>
<input type="email" name="email_address" class="email" value="" />
</label><br /><br />
<label>
<span>email Verification</span>
<input type="email" name="email_address_veri" value="" />
</label><br /><br />
<label>
<span>request</span>
<textarea name="request" required="required"></textarea>
</label><br /><br />
<a href="javascript:;" id="submit">submit</a>
</form>
</body>
</html>
输出