我有一种情况需要在提交之前验证表单。我目前正在使用jQuery validate插件。但是,根据单选按钮值,它只需要验证某些字段。
我认为能够重新初始化validate插件会起作用,但是当在表单上调用.valid时,它不会验证其他字段。
这是我用于验证初始化的jQuery。
var minVal = function () {
$("#user-details-form").validate({
errorClass: "text-danger",
rules: {
DeliveryFirstname: { required: true },
DeliverySurname: { required: true },
DeliveryCompanyName: { required: true },
DeliveryPostcode: { required: true, postcodeUK: true },
DeliveryAddress1: { required: true },
DeliveryCity: { required: true },
EmailAddress: { required: true, email: true },
Telephone: { required: true }
},
messages: {
DeliveryFirstname: { required: "First name required" },
DeliverySurname: { required: "Last name required" },
DeliveryCompanyName: { required: "Company name required" },
DeliveryPostcode: { required: "Postcode required", postcodeUk: "Invalid postcode" },
DeliveryAddress1: { required: "First line of address required" },
DeliveryCity: { required: "City required" },
EmailAddress: { required: "Email address required", email: "Invalid email address" },
Telephone: { required: "Telephone number required" }
}
});
};
var maxVal = function () {
$("#user-details-form").validate({
errorClass: "text-danger",
rules: {
DeliveryFirstname: { required: true },
DeliverySurname: { required: true },
DeliveryCompanyName: { required: true },
DeliveryPostcode: { required: true, postcodeUK: true },
DeliveryAddress1: { required: true },
DeliveryCity: { required: true },
InvoiceFirstname: { required: true },
InvoiceSurname: { required: true },
InvoiceCompanyName: { required: true },
InvoicePostcode: { required: true, postcodeUK: true },
InvoiceAddress1: { required: true },
InvoiceCity: { required: true },
EmailAddress: { required: true, email: true },
Telephone: { required: true }
},
messages: {
DeliveryFirstname: { required: "First name required" },
DeliverySurname: { required: "Last name required" },
DeliveryCompanyName: { required: "Company name required" },
DeliveryPostcode: { required: "Postcode required", postcodeUk: "Invalid postcode" },
DeliveryAddress1: { required: "First line of address required" },
DeliveryCity: { required: "City required" },
InvoiceFirstname: { required: true },
InvoiceSurname: { required: true },
InvoiceCompanyName: { required: true },
InvoicePostcode: { required: true, postcodeUK: true },
InvoiceAddress1: { required: true },
InvoiceCity: { required: true },
EmailAddress: { required: "Email address required", email: "Invalid email address" },
Telephone: { required: "Telephone number required" }
}
});
};
这是单选按钮更改方法(供参考)
$('input[name="IsInvoiceAddress"]:radio').on("change", function () {
var b = ($(this).val().toLowerCase() === "true");
if (!b) {
console.log("max val");
maxVal();
} else {
console.log("min val");
minVal();
}
});
据我所知,我可以简单地添加/删除每个元素的附加规则。使用.rules({})语法。但是想知道是否有办法在表单上重新初始化验证插件。
答案 0 :(得分:1)
据我所知,我可以简单地添加/删除每个元素的附加规则。使用
.rules({})
语法。但是想知道是否有办法在表单上重新初始化验证插件。
没有。没有重新初始化插件的方法。 .validate()
是初始化方法,它只在表单上调用一次。始终忽略所有后续调用。
.valid()
方法用于以编程方式随时强制进行验证测试。
开发人员为您提供.rules()
方法的要点是,可以更改规则而无需担心重新初始化。