[英]How to add new validations to Marketo form
由于不擅长 JS 和 Jquery,我正在努力向 Marketo 表单添加新的验证规则,当尝试提交表单时显示错误消息,将任何字段留空,我需要验证 FirstName 和 LastName 字段以仅允许字母字符,并且在输入数字字符时应该通过错误消息。 以下是我的 Marketo LP: http ://qliktest.qlik.com/Vinu-Test1_Reg_Form.html
这是自定义电子邮件验证的示例。 您可以将自定义代码放在 whenReady 函数中。
MktoForms2.whenReady(function(form) {
function isEmailValid(email) {
RE_EMAIL_ASCII_PUBLIC = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;
return RE_EMAIL_ASCII_PUBLIC.test(email);
}
form.onValidate(function() {
var values = form.vals();
if (values.Email) {
if (!isEmailValid(values.Email)) {
form.submitable(false);
var emailElem = form.getFormElem().find("#Email");
form.showErrorMessage(
// write your message here
"Must be valid email.",
emailElem
);
} else {
form.submitable(true);
}
}
});
如果您在 Marketo 中将字段标记为“必需”,则已经内置了逻辑来处理验证。 如果要创建一些自定义验证逻辑,IE 只允许字段中的字母字符,则需要使用 Marketo Forms 2.0 Javascript API (http://developers.marketo.com/documentation/websites/forms-2-0/ )
以下是使用 API 验证 Marketo 表单字段的示例:
MktoForms2.whenReady(function (form) {
//listen for the validate event form.onValidate(function() { // Get the values var vals = form.vals(); //Check your condition if (vals.Country == "USA" && vals.vehicleSize != "Massive") { // Prevent form submission form.submittable(false); // Show error message, pointed at VehicleSize element var vehicleSizeElem = form.getFormElem().find("#vehicleSize"); form.showErrorMessage("All Americans must have a massive vehicle", vehicleSizeElem); } else { // Enable submission for those who met the criteria form.submittable(true); } }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.