Javascript:表单在首次验证后和应用样式后提交

时间:2016-04-16 01:43:09

标签: javascript html forms validation email

我试图阻止某些用户ID从电子邮件中提交表单,例如黑名单,例如denieduser1@domain.com并使用denieduser1,当用户提交表单时,会出现一个引导警报,说明出错了,问题是如果再次单击该按钮,表单将被提交而不再进行验证,如果我删除了应用样式的部分并使用警报,那么它就可以了,所以这里是使用的代码:

表格:

var object = document.getElementByID("data");
object.onload = function() {
    var data = object.contentDocument.body.childNodes[0].innerHTML;
    // use the data
};

验证码:

<form id="contact" method="post" class="form" role="form" onsubmit="return validate()" action="m41lS3nd.php">

引导警报:

    function validate(){
    e.preventDefault;
var rejectList = [ "denied1" , "denied2" ]; //List of Blacklisted emails or domains
var emailValue = $('#email').val(); // To Get Value (can use getElementById)
var splitArray = emailValue.split('@'); // To Get Array

if(rejectList.indexOf(splitArray[0]) >= 0) //Check if contains any unwanted emails
{
// Means it has the rejected domains
document.getElementById("notification").style.display = "block"; //If unwanted emails are detected will show an alert
document.getElementById("notification").style.marginTop = "5px";
return false;

}else
var contactform = document.getElementById("contact"); //If good email is entered then get the form name and submits the form
contactform.submit();
return true;
}

提交按钮:

<!--Notification for invalid emails such as spam or unsolicited emails-->          
  <div id="notification" class="alert alert-warning alert-dismissible fade in" role="alert" style="display: none;">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  <span aria-hidden="true">&times;</span>
  </button>
  <strong>Oops!</strong>There is something wrong with your email, your reference code is: BL1SE.
  </div>

我从这篇文章中获取了代码并进行了改编,但无法发表评论或联系作者,也不知道出了什么问题:Code for validation

1 个答案:

答案 0 :(得分:0)

看起来您的Javascript语法不正确,因为e.preventDefault();指的是不存在的变量。我怀疑你的Javascript控制台报告错误。

我还强烈建议在{}之后添加else。我猜你不会按照你的想法行事。格式化以显示差异:

  } else
    var contactform = document.getElementById("contact"); 
  contactform.submit();
  return true;

与之相比:

  } else {
    var contactform = document.getElementById("contact"); 
    contactform.submit();
  }
  return true;

如果您添加了另一个未返回的else/if条件,那么您将点击contactform.submit();而不会声明并初始化contactform