简体   繁体   English

如何向 Marketo 表单添加新的验证

[英]How to add new validations to Marketo form

As am not good with JS and Jquery, am struggling to add new validation rule to the Marketo form, which shows error message when tried to submit the form leaving any field empty along with I need to validate the FirstName and LastName fields to allow only the alphabetic characters and should through a error message when numeric characters are entered.由于不擅长 JS 和 Jquery,我正在努力向 Marketo 表单添加新的验证规则,当尝试提交表单时显示错误消息,将任何字段留空,我需要验证 FirstName 和 LastName 字段以仅允许字母字符,并且在输入数字字符时应该通过错误消息。 Below is my Marketo LP: http://qliktest.qlik.com/Vinu-Test1_Reg_Form.html以下是我的 Marketo LP: http ://qliktest.qlik.com/Vinu-Test1_Reg_Form.html

Here is an example of custom email validation.这是自定义电子邮件验证的示例。 You can put the custom code in whenReady function.您可以将自定义代码放在 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);
        }
    }
});

If you mark the fields as "required" in Marketo there is already logic built in that will take care of the validation for you.如果您在 Marketo 中将字段标记为“必需”,则已经内置了逻辑来处理验证。 If you want to create some custom validation logic, IE only allowing alphabetic characters in the fields, you need to use the Marketo Forms 2.0 Javascript API (http://developers.marketo.com/documentation/websites/forms-2-0/ )如果要创建一些自定义验证逻辑,IE 只允许字段中的字母字符,则需要使用 Marketo Forms 2.0 Javascript API (http://developers.marketo.com/documentation/websites/forms-2-0/ )

Here's an example of validating a Marketo form field using the API:以下是使用 API 验证 Marketo 表单字段的示例:

MktoForms2.whenReady(function (form) { 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM