简体   繁体   English

用于多个用户输入的表单验证的 JavaScript

[英]JavaScript for Form Validation For Multiple User Inputs

I have got a JavaScript whose function is to make sure that the email address users provide on my form, matches with the result (Email address) in my JavaScript before my form submits itself.我有一个 JavaScript,它的功能是确保用户在我的表单上提供的电子邮件地址在我的表单提交之前与我的 JavaScript 中的结果(电子邮件地址)匹配。

(Auto Response) I use this JavaScript because I designed my form to send a copy of each user's response to their email address. (自动回复)我使用这个 JavaScript 是因为我设计了我的表单来将每个用户回复的副本发送到他们的电子邮件地址。

The JavaScript that I have can only validate the input of one user meaning I have to create a separate form for each user and put the JavaScript containing the email address of each user.我拥有的 JavaScript 只能验证一个用户的输入,这意味着我必须为每个用户创建一个单独的表单,并放置包含每个用户电子邮件地址的 JavaScript。

How can I use just one JavaScript on a single form to validate multiple users email address before my form submits itself?如何在表单提交之前仅在单个表单上使用一个 JavaScript 来验证多个用户的电子邮件地址?

Below is my single JavaScript for a single form code;下面是我用于单个表单代码的单个 JavaScript;

 <script type="text/javascript"> function check_email() { var email = document.getElementById("email").value; if(email.localeCompare("badmansmo@gmail.com")) { alert("ERROR. Email does not match."); return false; } return true; }
 <form action='' method='POST' enctype="multipart/form-data" onsubmit="return check_email();"> <div class='item'> <p><b><span style="color: red;">Email Address</span></b><span class='required'>*</span></p> <input type="email" name="email" placeholder="ba******o@***.com" required='' id="email"/> </div> <div class='question'> <center><p>Privacy Policy<span class='required'>*</span></p></center> <div class='question-answer checkbox-item'> <div> <center><label class='check' for='check_1'><span>By submitting this form you agree to the terms of service <a href='https://www.google.com/p/privacy-policy.html' target="_blank">privacy policy.</a></span></label></center> </div> </div> </div> <div class='btn-block'> <center> <button href='/' type='submit' id="submitForm">Submit</button></center> </div></form>

Below is a sample of the JavaScript that I have tried but didn't work;下面是我尝试过但没有工作的 JavaScript 示例;

 <script type="text/javascript"> function check_email() { var email = document.getElementById("email").value; if(email.localeCompare == "badmansmo@gmail.com" || email.localeCompare == "billysam@gmail.com")) { alert("ATTENTION! \\nThe email address that you provided did not match with the email that is associated with your account."); return false; } return true; }

You can do something like this:你可以这样做:

const EMAIL_LIST = ["badmansmo@gmail.com", "billysam@gmail.com"];

function check_email() {
  const email = document.getElementById("email").value;

  if (!EMAIL_LIST.includes(email)) {
    alert(
      "ATTENTION! \nThe email address that you provided did not match with the email that is associated with your account."
    );
    return false;
  }

  return true;
}

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

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