简体   繁体   English

在发送数据之前验证ajax提交post方法

[英]validate ajax submit post method before sending data

In this particular case I'm using google tag manager to submit a form within a bootstrap modal. 在这种情况下,我正在使用Google Tag Manager在Bootstrap模式下提交表单。
I don't have access to the backend and this is why I'm using GTM to target a button and use the onClick. 我无权访问后端,这就是为什么我使用GTM定位按钮并使用onClick的原因。

<script>
$(document).on('submit','#MyForm',function(event){
  var form = $('#MyForm');
  var submit = $('#ssend_btn');
  form.on('submit', function(e) {
    e.preventDefault();
    if($('input', this).val().trim() == ''  ){
      //handle error message
      alert("im empty and email will not send");
    }
    else if (submit != 'null' ){
      event.preventDefault()
      var formData = $(this).serialize();
      console.log(formData);
      $.ajax({
        url: "page.php", // some php
        data: formData,
        datatype: "json",
        type: "POST",
        success: function(data) {

        }
      });
    }
  });
});
</script>

After the email is sent, the modal must not close and this is why I'm using ajax. 发送电子邮件后,模态必须不能关闭,这就是为什么我使用ajax。
If I remove the validation I can send email, but even blank it will submit. 如果删除验证,我可以发送电子邮件,但即使是空白也将提交。
I have other validation with javascript, but is not respecting it. 我使用javascript进行了其他验证,但不尊重它。

<script>
function validateForm() {
  var x = document.forms["myForm"]["EMAIL"].value;
  var atpos = x.indexOf("@");
  var dotpos = x.lastIndexOf(".");
  if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
    error = "testingo";
    document.getElementById("errorid").innerHTML = error;
    return false;
  }

</script>

on console log this is what i get 在控制台日志上,这就是我得到的 在此处输入图片说明 how wever the email is sent 电子邮件的发送方式

Are you looking for something like this? 您是否正在寻找这样的东西? Looks like you're using a mixture of vanilla JS and jQuery; 看起来您正在使用香草JS和jQuery的混合物; I'd suggest sticking to one or the other when trying to reference the form and the form inputs, to make it easier.. Also, if you change your input's type to "email" instead of "text", built in browser functionality (for Chrome, etc) will help to ensure a valid email is entered in addition to your validation logic. 建议您在尝试引用表单和表单输入时坚持使用一个或另一个,以使其更容易。此外,如果将输入的类型更改为“电子邮件”而不是“文本”,则内置浏览器功能(适用于Chrome等)将有助于确保除了验证逻辑之外,还输入了有效的电子邮件。

 function validateForm() { if ($('#EMAIL').val().trim() === '') { return false; } var x = $('#EMAIL').val(); var atpos = x.indexOf("@"); var dotpos = x.lastIndexOf("."); if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) { error = "testingo"; document.getElementById("errorid").innerHTML = error; return false; } return true; } $(document).ready(function() { var form = $('#MyForm'); var submit = $('#ssend_btn'); form.on('submit', function(e) { e.preventDefault(); if (validateForm() === false) { //handle error message alert("im empty and email will not send"); } else { var formData = $(this).serialize(); console.log(formData); $.ajax({ url: "page.php", // some php data: formData, datatype: "json", type: "POST", success: function(data) { } }); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="MyForm" name="MyForm"> <input type="text" name="EMAIL" id="EMAIL" placeholder="EMAIL" /> <div id="errorid"></div> <input type="submit" id="ssend_btn" value="Submit" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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