简体   繁体   中英

How can I make an array or variable for an if statement in javascript?

I am trying to make a login system using if statements and input values and I want to know if there is a way I can create something like an array or variable or object that I can put all the passwords and usernames in and that I can also add new ones easily. I am not actually using this for a website; it is just a project. Thank you.

$(document).ready(function(){
    $('.login').click(function(){
        var userResult = $('input[name=userInput]').val();
        var passResult = $('input[name=passInput]').val();
        if(userResult === "CodeAcademy" && passResult === "fun-coding" || userResult === "User_Example" && passResult === "Pass_Example")
        {
          //Some stuff that will happen
        }
    });
});

FYI userInput and passInput are the input boxes that the user puts their username and password into.

You can avoid long chains of logic, array iteration, extra string quotes, and slow searching by using a look-up table Object to store many logins and passwords in one simple little data structure:

$(document).ready(function(){
    $('.login').click(function(){
        var userResult = $('input[name=userInput]').val();
        var passResult = $('input[name=passInput]').val();
        if({  // username     password
               CodeAcademy:  "fun-coding", 
               User_Example: "Pass_Example",
               "User with space": "sOmeP@ssw0rd",
        }[  userResult  ] === passResult ){ // user valid and password match?
            alert("authenticated"); //Some stuff that will happen
        }//end if valid user?
    });
});

You can create an array of login objects using a separate form, and using grep to search through this array to see if there is a matching user. Here is demo https://jsfiddle.net/f4aqmt4e/

Below is the code and the explanation:

var myLogins = [];//this might be object that you store the login information

//lets say when a new user registers you keep record of their login information in myLogins
//first create the login information
var myLogin = new Object();
myLogin.Username = "myusername";
myLogin.Password= "12345";

//then add the login information to the login-store array
myLogins.push(myLogin);


 $('.login').click(function(){
    var userResult = $('input[name=userInput]').val();
    var passResult = $('input[name=passInput]').val();

    //grep will help you to find a certain element within an array based on a condition
    //we will use grep to see if there a matching login info
    var loginresult = $.grep(myLogins, function(e){ return e.Username == userResult  && e.Password == passResult ; });

    if(loginresult.lenght)
    {
       //login successful
    }else {
       //login failed
    }   
 });

Try utilizing Array.prototype.every()

 $(document).ready(function() { var vals = [ ["CodeAcademy", "User_Example"], ["fun-coding", "Pass_Example"] ]; $(".login").click(function() { var res = $("._login").toArray().every(function(el, i) { return i === 0 ? $.inArray(el.value, vals[i]) !== -1 // name : $.inArray(el.value, vals[i]) !== -1 // pass }); if (res) { // do stuff alert("login successful") }; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <input name="userInput" type="text" class="_login" /> <input name="passInput" type="text" class="_login" /> <input type="button" class="login" value="login" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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