我可以用jquery拦截marketo登陆页面表单提交

时间:2014-05-03 17:45:16

标签: jquery marketo

是否可以通过查询拦截marketo登陆页面表单提交, 我正在尝试运行自定义表单验证,以防止提交表单。 任何想法?

2 个答案:

答案 0 :(得分:2)

这是可能的。 Marketo Forms提供了一个名为onValidate的方法。此方法允许您添加在表单提交期间触发的自定义验证。以下是如何使用onValidate方法阻止提交非公司电子邮件地址的示例。

<script>
(function (){
  // Please include the email domains you would like to block in this list
  var invalidDomains = ["@gmail.","@yahoo.","@hotmail.","@live.","@aol.","@outlook."];

  MktoForms2.whenReady(function (form){
    form.onValidate(function(){
      var email = form.vals().Email;
      if(email){
        if(!isEmailGood(email)) {
          form.submitable(false);
          var emailElem = form.getFormElem().find("#Email");
          form.showErrorMessage("Must be Business email.", emailElem);
        }else{
          form.submitable(true);
        }
      }
    });
  });

  function isEmailGood(email) {
    for(var i=0; i < invalidDomains.length; i++) {
      var domain = invalidDomains[i];
      if (email.indexOf(domain) != -1) {
        return false;
      }
    }
    return true;
  }

})();
</script>

答案 1 :(得分:0)

桑德福德怀特曼在Marketo论坛上回答了这个问题;

var instanceURL = '//app-sj01.marketo.com',
  munchkinId = '410-XOR-673',
  formId = 734;
MktoForms2.loadForm(instanceURL, munchkinId, formId);

MktoForms2.whenReady(function(form) {
  var companyURLRules = {
        formFieldName : 'Website',
    allowedHostnames : ['info.example.com', 'marketo.com'],
        errorMessage : 'You must use an approved Domain.'
    };

  /* ---- NO NEED TO EDIT BELOW THIS LINE! ---- */

  var formJq = form.getFormElem(),
    urlJq = formJq.find('[name="' + companyURLRules.formFieldName + '"]');

  form.onValidate(function(native) {
    if (!native) return;

        var currentVals = form.getValues();

    form.submittable(false);
    if (!hostnameInList(currentVals[companyURLRules.formFieldName], companyURLRules.allowedHostnames)) {
      form.showErrorMessage(companyURLRules.errorMessage, urlJq);
    } else {
      form.submittable(true);
    }
  });


  function hostnameInList(url, list) {
    var loc = document.createElement('a');

    loc.href = /^https?:\/\//.test(url) ? url : 'https://' + url;
    return !!list.filter(function(itm) {
      return loc.hostname == itm;
    }).length;
  }

});