如何阻止特定的电子邮件地址

How to block specific email addresses

我正在使用 jQuery Validation Plugin,我需要防止某些垃圾邮件地址停止发送经常性潜在客户。

尝试了 Google 并阅读了插件文档,但我是 JS 的初学者,所以我找不到任何我需要的东西。

P.S。不是谈论无效的电子邮件地址,而是经常出现的有效电子邮件地址。

您可以在您的数据库中创建一个 table 来保存您不需要的电子邮件和域,并在执行下一步之前检查它们。

Please keep in mind that you should always double check user input serverside. Malicious users will always be able to bypass rules that are set clientside. Also, by doing it in JavaScript, you are disclosing your list of banned emails, and making it easier for hackers to see what they should do to bypass your rules.

也就是说,您可以加载 jQuery Validate's Additional methods plugin and use addMethod 来创建自己的规则:

// Banning domains
var bannedDomains = ["spam.com", "junk.com"];

$.validator.addMethod('domainNotBanned', function(value, elem, param) {
  var domain = value.split('@')[1];
  return bannedDomains.indexOf(domain) < 0;
}, 'Emails from this domain are not allowed.');

// Banning specific addresses
var bannedEmails = ["mean@hacker.com", "kim.kardashian@gmail.com"];

$.validator.addMethod('emailNotBanned', function(value, elem, param) {
  return bannedEmails.indexOf(value) < 0;
}, 'This email address is banned.');

// Applying these rules
$('#myForm').validate({
  rules: {
    email: {
      required: true,
      email: true,
      domainNotBanned: true,
      emailNotBanned: true
    }
  }
});

// Just for the demo
$('#myForm').on('submit', function(e) {
  e.preventDefault();
  alert("This email is valid.");
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>

<form id="myForm" name="myForm" action="#" method="POST">                                                                                                                                                                                                                     <style>#email-error { display: block; color: red; }</style>
  <label for="email">Email: </label>
  <input id="email" name="email" type="email" minlength="6"/><br>
  <input type="submit" value="Submit">
</form>