Home > OS >  JavaScript for Form Validation For Multiple User Inputs
JavaScript for Form Validation For Multiple User Inputs

Time:10-19

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.

(Auto Response) I use this JavaScript because I designed my form to send a copy of each user's response to their email address.

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.

How can I use just one JavaScript on a single form to validate multiple users email address before my form submits itself?

Below is my single JavaScript for a single form code;

<script type="text/javascript">
function check_email()
{
    var email = document.getElementById("email").value;
    
    if(email.localeCompare("[email protected]")) {
        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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Below is a sample of the JavaScript that I have tried but didn't work;

<script type="text/javascript">
function check_email()
{
    var email = document.getElementById("email").value;
    
    if(email.localeCompare == "[email protected]" || email.localeCompare == "[email protected]")) {
      
        alert("ATTENTION! \nThe email address that you provided did not match with the email that is associated with your account.");
        return false;
    }
    return true;
}
    
          
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can do something like this:

const EMAIL_LIST = ["[email protected]", "[email protected]"];

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;
}
  • Related