Question: I have a list of valid user. I want that only these user will be able to send post request all other user should be identified as invalid user. I have written javascript code but unable to make invalid user to stop the execution of programs.

```js
 function validateForm() {
        var x = document.forms["myForm"]["email"].value;
        var atpos = x.indexOf("@");
        var dotpos = x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
            alert("Not a valid e-mail address");
            return false;
        }
        var eMailList = ["hankev@gmail.com", "slurp@gmail.com", "cofo@gmail.com", "vecrify@gmail.com"];
        var i;
        for (i=0; i< eMailList.length; i++){
         if(x != eMailList[i]){
      alert("Not a valid user");
         return false;
            }

        }
    }
```

```html
 <form name="myForm" action="/action_page_post.php" onsubmit="return validateForm();" method="post">
    Email: <input type="text" name="email">
    <input type="submit" value="Submit">
</form>
```

Answer: 
At first if you care about security issues, you shouldnt do it on the javascript side.

See code sample below:

```js
function validateForm() {
    var x = document.forms["myForm"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Not a valid e-mail address");
        return false;
    }
    var eMailList = ["hankev@gmail.com", "slurp@gmail.com", "cofo@gmail.com", "vecrify@gmail.com"];
    var i;
 if($.inArray(x,eMailList) == -1)
 {
  alert("Not a valid user");
 }
 else
 {
  alert("valid user");
 }
}
```

```html
<form name="myForm" action="/action_page_post.php" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
```

Link to [JsFiddle](https://jsfiddle.net/guutes7m/)
There is no need to loop through the array you can just use `if(eMailList.indexOf(x) < 0)` implemented below. The reason your code would always returns false is because that even if you enter a valid email it is not equal to every other item in the array.

```js
 function validateForm() {
        var x = document.forms["myForm"]["email"].value;
        var atpos = x.indexOf("@");
        var dotpos = x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
            alert("Not a valid e-mail address");
            return false;
        }
        var eMailList = ["hankev@gmail.com", "slurp@gmail.com", "cofo@gmail.com", "vecrify@gmail.com"];

        if(eMailList.indexOf(x) < 0){
      alert("Not a valid user");
         return false;
        }
    }
```

```html
 <form name="myForm" action="/action_page_post.php" onsubmit="return validateForm();" method="post">
    Email: <input type="text" name="email">
    <input type="submit" value="Submit">
</form>
```