0

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>

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

4
  • When a user submits your form you should rather check in your database if that email exists or not. The way you are doing is not at all correct because this is the client-side code that any user can see using the browser dev tools. Commented Oct 18, 2021 at 7:27
  • I still need the solution. I am aware that anyone can see using browser dev tools. Commented Oct 18, 2021 at 7:29
  • Then just make an array of the email addresses that you want to validate and check if the user entered email is in that array or not. Commented Oct 18, 2021 at 7:31
  • That's is the solution that I am seeking. If you went through my question diligently, you'd see that I am asking How do I make an array of the email addresses that I want to validate and check if the user entered email is in that array or not. Commented Oct 18, 2021 at 7:39

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.